Python Programming

92 flashcards by MentisLibrary

Syntax, data types, comprehensions, decorators, classes, and the standard library, with a runnable snippet on nearly every card. Includes the idioms and the classic gotchas that trip people up.

What are Python's built-in numeric types?
int (arbitrary precision), float (double precision), and complex.
Difference between a list and a tuple
A list is mutable and written with square brackets; a tuple is immutable and written with parentheses.
What is a dict?
A mutable mapping from hashable keys to arbitrary values, with average constant time lookup.
What is a set?
An unordered collection of unique hashable elements supporting fast membership tests and set algebra.
List comprehension syntax
[expression for item in iterable if condition] builds a new list in one expression.
Dict comprehension
{key_expr: value_expr for item in iterable} builds a dict.
Generator expression
Same syntax as a list comprehension but with parentheses, producing values lazily instead of building a list.
What does the walrus operator do?
The assignment expression := binds a value to a name as part of a larger expression.
Slicing syntax
sequence[start:stop:step] returns a new sequence from start up to but not including stop.
How do you reverse a sequence with a slice?
Use a step of -1.
Difference between is and ==
== compares values; is compares object identity, meaning whether two names point to the same object.
Why is x is 1000 unreliable?
CPython interns small integers, roughly -5 to 256, so identity accidentally works for them but not for larger values.
What is a mutable default argument bug?
A default argument is evaluated once at function definition, so a mutable default is shared across all calls.
What does *args do?
It collects any extra positional arguments into a tuple.
What does **kwargs do?
It collects any extra keyword arguments into a dict.
Keyword-only arguments
Parameters after a bare * in the signature can only be passed by name.
Positional-only parameters
Parameters before a / in the signature can only be passed positionally.
What is a lambda?
An anonymous function limited to a single expression, whose value is returned.
What is a decorator?
A callable that takes a function and returns a replacement, applied with @ syntax above the definition.
What does functools.wraps do?
Copies the wrapped function's name, docstring, module, and annotations onto the wrapper.
What is a closure?
A nested function that keeps access to variables from the enclosing scope after that scope has returned.
What does the nonlocal keyword do?
It rebinds a name in the nearest enclosing function scope rather than creating a new local.
What does global do?
It declares that assignments to a name inside a function should rebind the module-level variable.
What is the LEGB rule?
Name lookup order: Local, then Enclosing, then Global, then Builtins.
What is a generator function?
A function containing yield, which returns an iterator producing values one at a time and pausing between them.
What does yield from do?
Delegates iteration to a sub-iterator, forwarding its values and its return value.
Iterator protocol
An iterable has __iter__; an iterator has __iter__ and __next__ and raises StopIteration when exhausted.
What is a context manager?
An object defining __enter__ and __exit__ so the with statement can guarantee setup and cleanup.
How do you write a context manager quickly?
Use the contextlib.contextmanager decorator on a generator that yields exactly once.
f-string basics
A string literal prefixed with f, where expressions inside braces are evaluated and formatted.