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 is a 'free variable' in the context of Python closures? → A variable used in a function but defined in an enclosing outer scope
- 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.
- Which of the following is NOT a built-in Python decorator? → @private
- What will be the output of the following Python code? x = 5 y = '10' print(x + int(y)) → 15
- 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
- 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.
- 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
- What does the `yield` keyword do in a Python function? → Pauses execution and returns a value to the caller, resuming on next call
- What is the value of continuing education in core syntax for Python professionals? → It keeps professionals current with evolving standards and practices
- 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`
- What does @staticmethod mean when applied to a method inside a class? → The method receives no implicit first argument — no self or cls
- What does the following code output? sampleSet = {"Jodi", "Eric", "Garry"} sampleSet.add(1, "Vicki") print(sampleSet) → The program executed with error
- In Python's scope resolution, what is the LEGB rule? → The sequence Python follows to find a variable: Local, Enclosing, Global, Built-in.
- What does the following code output? valueOne = 5 ** 2 valueTwo = 5 ** 3 print(valueOne) print(valueTwo) → 25 125
- What does `'a,b,c'.split(',')` return? → ['a','b','c']
- What is the output of `list(x for x in range(5) if x % 2 == 0)`? → [0, 2, 4]
- What specifically is PYTHONPATH? → It tells the Python interpreter where to locate the module files imported into a program
- What does the following code output? sampleList = ["Jon", "Kelly", "Jessa"] sampleList.append(2, "Scott") print(sampleList) → The program executed with errors
- Which professional attribute is most valued in core syntax within the Python field? → Accountability and commitment to standards
- What is the output of `sum(x for x in range(1, 6))`? → 15
- What is the purpose of `itertools.chain()`? → Chains multiple iterables together into one continuous iterator
- Which of the following `while` loops will result in an infinite loop? → i = 0 while i < 5: i = 1
- Which of the following is a valid dict comprehension? → {k: v for k, v in items}
- Which of these naming conventions for Python identifiers is incorrect? → An identifier can start with a number
- Consider the following code snippet: def my_func(): x = 10 print(x) x = 20 my_func() print(x) → 10 20
- What does functools.wraps do when used inside a decorator? → Copies the original function's metadata (like __name__ and __doc__) to the wrapper
- What is the output of `[x for x in [1,2,3,4,5] if x > 3]`? → [4, 5]
- Which of the following data types is immutable in Python? → tuple
- 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: