Twenty flashcards covering the Python language essentials that appear in coursework, technical interviews and certification exams. Includes mutable versus immutable types, lists, tuples, dictionaries and sets, slicing, comprehensions, function arguments, LEGB...
Twenty flashcards covering the Python language essentials that appear in coursework, technical interviews and certification exams. Includes mutable versus immutable types, lists, tuples, dictionaries and sets, slicing, comprehensions, function arguments, LEGB scope, exception handling, context managers, classes and inheritance, generators, decorators and the Global Interpreter Lock.
Ready to test yourself?
Flip through all 20 cards in interactive study mode.
Lists are mutable and use []; tuples are immutable, use (), are slightly faster and can serve as dictionary keys.
A hash-table mapping of unique keys to values. Average lookup, insertion and deletion are O(1). Since Python 3.7 insertion order is guaranteed.
Storing unordered collections of unique hashable items, with fast membership testing and mathematical operations such as union, intersection and difference.
sequence[start:stop:step] — start is inclusive, stop exclusive. s[::-1] reverses a sequence and s[:] produces a shallow copy.
[x**2 for x in range(10) if x % 2 == 0] — more concise and usually faster than an equivalent explicit loop with append.
The default is evaluated once at definition and shared across calls, so it accumulates state. Use def f(items=None): items = items or [] instead.
*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dictionary.
A single-expression anonymous function, e.g. lambda x, y: x + y. It cannot contain statements and is typically used as a short inline callback.
Names are resolved in order: Local, Enclosing (nested function), Global (module) and Built-in.
try holds risky code, except handles specific exceptions, else runs only if no exception occurred, and finally always runs for cleanup.
It uses a context manager to acquire and automatically release a resource, guaranteeing cleanup even if an exception is raised — e.g. with open(f) as fh:.
self is the explicit reference to the current instance. __init__ is the initialiser, called automatically after object creation to set up instance attributes.
Returns a proxy to the parent class, letting a subclass invoke the parent's implementation — most commonly super().__init__() within an overridden initialiser.
__str__ returns a readable string for end users (used by print). __repr__ returns an unambiguous developer-facing representation (used in the REPL and debugging).
A function using yield that produces values lazily one at a time, preserving state between calls. It uses constant memory regardless of sequence length.
A callable that wraps another function to extend its behaviour without modifying its source, applied with @decorator syntax. Common uses: logging, timing, caching, access control.
is tests identity (same object in memory); == tests equality of value. Use is only with singletons such as None.
The Global Interpreter Lock permits only one thread to execute Python bytecode at a time. Threads still help with I/O-bound work; use multiprocessing for CPU-bound work.