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
  1. What does a `try/except/finally` block guarantee about the `finally` clause? It always runs regardless of exceptions
  2. When is a Python function's default argument value evaluated? At function definition time
  3. How can you write a one-line if statement in Python? if cond: stmt
  4. Which statement about Python dictionaries is TRUE as of Python 3.7+? Insertion order is preserved
  5. What is the role of `setup.py` in a Python package? It defines package metadata and build instructions for distribution
  6. What is a 'free variable' in the context of Python closures? A variable used in a function but defined in an enclosing outer scope
  7. What does the `//` operator do in Python? Performs floor (integer) division
  8. What is the purpose of `if __name__ == '__main__':` in a Python script? Both B and C
  9. Given `d = {'x': 10, 'y': 20}`, what is `list(d)`? ['x', 'y']
  10. What is the value of `int(3.9)`? 3
  11. 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')
  12. In object-oriented programming, what is the fundamental relationship between a class and an object? A class is a blueprint for creating objects.
  13. What does `iter()` return when called on a list? A list iterator object
  14. Which statement about Python's `set` type is correct? Sets are unordered and contain unique elements
  15. Which of the following correctly merges dict `b` into dict `a`, overwriting duplicate keys with `b`'s values (Python 3.9+)? a | b
  16. Which of the following is NOT a built-in Python decorator? @private
  17. What does the `__all__` variable in a module control? Which names are exported when `from module import *` is used
  18. What does `set.issubset()` return when called as `{1, 2}.issubset({1, 2, 3})`? True
  19. What is the output of `print(not not True)`? True
  20. What happens when you call `del obj.attr` where `attr` has a `@property` setter but no deleter? Python raises an AttributeError
  21. What will be the output of the following Python code? x = 5 y = '10' print(x + int(y)) 15
  22. Which exception is raised by `assert x > 0` when x is -1? AssertionError
  23. 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
  24. What is the output of this walrus-operator expression (Python 3.8+)? data = [1, 2, 3] if n := len(data): print(n) 3
  25. What does `s = {1, 2, 3}; s.discard(5)` do? Does nothing silently
  26. Which keyword is used to define an anonymous function in Python? lambda
  27. What does `x = []; x or 'default'` return? 'default'
  28. What is the result of `{1, 2} | {3, 4}`? {1, 2, 3, 4}
  29. What does the `*args` syntax in a function definition allow? Accept any number of positional arguments
  30. Which of the following correctly uses `not` in an if statement? if not x == 5:
Was this helpful?