Python Cheat Sheet 2026
The 30 highest-yield Python facts, distilled from real exam questions. Print it, save it as a PDF, or study it here — free, no sign-up.
40 questions
65 min time limit
70% to pass
- What does a `try/except/finally` block guarantee about the `finally` clause? → It always runs regardless of exceptions
- When is a Python function's default argument value evaluated? → At function definition time
- How can you write a one-line if statement in Python? → if cond: stmt
- Which statement about Python dictionaries is TRUE as of Python 3.7+? → Insertion order is preserved
- What is the role of `setup.py` in a Python package? → It defines package metadata and build instructions for distribution
- What is a 'free variable' in the context of Python closures? → A variable used in a function but defined in an enclosing outer scope
- What does the `//` operator do in Python? → Performs floor (integer) division
- What is the purpose of `if __name__ == '__main__':` in a Python script? → Both B and C
- Given `d = {'x': 10, 'y': 20}`, what is `list(d)`? → ['x', 'y']
- What is the value of `int(3.9)`? → 3
- Which of the following is the most idiomatic and safest way to open a file for writing in Python, ensuring that it is always closed properly? → with open('data.txt', 'w') as file: file.write('content')
- In object-oriented programming, what is the fundamental relationship between a class and an object? → A class is a blueprint for creating objects.
- What does `iter()` return when called on a list? → A list iterator object
- Which statement about Python's `set` type is correct? → Sets are unordered and contain unique elements
- Which of the following correctly merges dict `b` into dict `a`, overwriting duplicate keys with `b`'s values (Python 3.9+)? → a | b
- Which of the following is NOT a built-in Python decorator? → @private
- What does the `__all__` variable in a module control? → Which names are exported when `from module import *` is used
- What does `set.issubset()` return when called as `{1, 2}.issubset({1, 2, 3})`? → True
- What is the output of `print(not not True)`? → True
- What happens when you call `del obj.attr` where `attr` has a `@property` setter but no deleter? → Python raises an AttributeError
- What will be the output of the following Python code? x = 5 y = '10' print(x + int(y)) → 15
- Which exception is raised by `assert x > 0` when x is -1? → AssertionError
- What is the purpose of the nonlocal keyword in Python closures? → To allow an inner function to modify a variable in the enclosing (non-global) scope
- What is the output of this walrus-operator expression (Python 3.8+)? data = [1, 2, 3] if n := len(data): print(n) → 3
- What does `s = {1, 2, 3}; s.discard(5)` do? → Does nothing silently
- Which keyword is used to define an anonymous function in Python? → lambda
- What does `x = []; x or 'default'` return? → 'default'
- What is the result of `{1, 2} | {3, 4}`? → {1, 2, 3, 4}
- What does the `*args` syntax in a function definition allow? → Accept any number of positional arguments
- Which of the following correctly uses `not` in an if statement? → if not x == 5:
Turn these facts into recall:
Was this helpful?