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 is a 'free variable' in the context of Python closures? A variable used in a function but defined in an enclosing outer scope
  2. 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')
  3. In object-oriented programming, what is the fundamental relationship between a class and an object? A class is a blueprint for creating objects.
  4. Which of the following is NOT a built-in Python decorator? @private
  5. What will be the output of the following Python code? x = 5 y = '10' print(x + int(y)) 15
  6. 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
  7. Which statement correctly describes the fundamental difference between a set and a dictionary? A set stores unique, individual elements, while a dictionary stores key-value pairs.
  8. Can a decorator be applied to a class definition in Python? Yes, class decorators receive the class as their argument and return a modified class
  9. What does the `yield` keyword do in a Python function? Pauses execution and returns a value to the caller, resuming on next call
  10. What is the value of continuing education in core syntax for Python professionals? It keeps professionals current with evolving standards and practices
  11. What is the correct order of execution for the blocks in a `try...except...else...finally` statement when no exception occurs within the `try` block? `try` -> `else` -> `finally`
  12. What does @staticmethod mean when applied to a method inside a class? The method receives no implicit first argument — no self or cls
  13. What does the following code output? sampleSet = {"Jodi", "Eric", "Garry"} sampleSet.add(1, "Vicki") print(sampleSet) The program executed with error
  14. In Python's scope resolution, what is the LEGB rule? The sequence Python follows to find a variable: Local, Enclosing, Global, Built-in.
  15. What does the following code output? valueOne = 5 ** 2 valueTwo = 5 ** 3 print(valueOne) print(valueTwo) 25 125
  16. What does `'a,b,c'.split(',')` return? ['a','b','c']
  17. What is the output of `list(x for x in range(5) if x % 2 == 0)`? [0, 2, 4]
  18. What specifically is PYTHONPATH? It tells the Python interpreter where to locate the module files imported into a program
  19. What does the following code output? sampleList = ["Jon", "Kelly", "Jessa"] sampleList.append(2, "Scott") print(sampleList) The program executed with errors
  20. Which professional attribute is most valued in core syntax within the Python field? Accountability and commitment to standards
  21. What is the output of `sum(x for x in range(1, 6))`? 15
  22. What is the purpose of `itertools.chain()`? Chains multiple iterables together into one continuous iterator
  23. Which of the following `while` loops will result in an infinite loop? i = 0 while i < 5: i = 1
  24. Which of the following is a valid dict comprehension? {k: v for k, v in items}
  25. Which of these naming conventions for Python identifiers is incorrect? An identifier can start with a number
  26. Consider the following code snippet: def my_func(): x = 10 print(x) x = 20 my_func() print(x) 10 20
  27. What does functools.wraps do when used inside a decorator? Copies the original function's metadata (like __name__ and __doc__) to the wrapper
  28. What is the output of `[x for x in [1,2,3,4,5] if x > 3]`? [4, 5]
  29. Which of the following data types is immutable in Python? tuple
  30. What is the primary role of the `__init__` method in a Python class? To initialize the state of a new object when it is created.
Turn these facts into recall: