What Is a Generator in Python and How Do You Use It?
A Python generator uses yield to produce values lazily with constant memory. Learn generator functions, expressions, pipelines and common pitfalls.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
A Python generator uses yield to produce values lazily with constant memory. Learn generator functions, expressions, pipelines and common pitfalls.
The Python not operator inverts a truth value and always returns True or False. See truthy and falsy values, precedence, not in and common pitfalls.
The Python or operator returns the first truthy operand and short-circuits. See how to use it in conditions and defaults, and which mistakes to avoid.
The and keyword is Python's logical AND. It short-circuits and returns one of its operands, which is useful for guarding conditions.
Learn what is not does in Python, how identity differs from value inequality, and why x is not None is the idiomatic check for optional values.
Learn what the Python is operator does, how identity differs from equality, why to use is None, and why comparing values with is is unreliable.
How Python's nonlocal keyword lets a nested function rebind an enclosing variable, with closure examples and the difference from global.
The global keyword lets a function rebind a module-level name. See how scope works, why UnboundLocalError happens and what to use instead.
yield turns a function into a lazy generator that pauses and resumes. Learn how it differs from return, with pipelines, yield from and send examples.
assert raises AssertionError when a condition is false. It is a debugging aid for internal assumptions, not for input validation, since -O removes it.
A module is one .py file and a package is a directory of modules. See how imports, __init__.py and relative imports work, with examples.
A Python decorator wraps a function to add behaviour such as logging or caching. See how to write, stack and type decorators with working examples.
A Python lambda is an anonymous one-expression function. See syntax, key functions, map and filter use, limitations, and when to prefer def.
A shallow copy reuses nested objects, while a deep copy duplicates everything. See copy.copy and copy.deepcopy in Python with examples and common pitfalls.
break ends the nearest loop and continue skips to the next iteration. Examples for for and while loops, nested loops and the loop else clause.
The Python for loop iterates over any iterable. See range, enumerate, zip, break, continue, loop else and the pitfalls to avoid.
A while loop repeats a block while its condition holds. Learn the syntax, break and continue, the else clause and how to avoid infinite loops.
The else block runs when no earlier condition matched. See how it works with if, loops and try, plus the one-line conditional expression.
The elif statement tests extra conditions after an if and runs only the first true branch. See syntax, ordering rules, examples and common mistakes.
The if statement runs code only when a condition is true. Learn syntax, elif and else, truthiness, operators, match and common errors.