Python Flashcards
7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Python flashcards as text
What is the difference between `@staticmethod` and `@classmethod` in Python?
Answer: @classmethod receives the class as first arg, @staticmethod receives no implicit arg
`@classmethod` receives the class (`cls`) as its first argument, while `@staticmethod` receives no implicit first argument.
What will `[x**2 for x in range(5) if x % 2 == 0]` evaluate to?
Answer: [0, 4, 16]
The comprehension squares only even numbers from 0–4: 0²=0, 2²=4, 4²=16.
Which module provides support for regular expressions in Python?
Answer: re
The built-in `re` module provides functions like `re.search()`, `re.match()`, and `re.sub()` for working with regular expressions.
What happens when you use `open('file.txt', 'a')` mode?
Answer: Opens for appending; writes go to end without erasing existing content
Mode `'a'` opens a file for appending, creating it if it doesn't exist, and all writes go to the end.
What does `enumerate()` return when iterating over a list?
Answer: Tuples of (index, value)
`enumerate()` yields `(index, element)` tuples, letting you track both position and value in a loop.
Which of the following correctly creates a shallow copy of a list `lst`?
Answer: lst2 = lst.copy()
`lst.copy()` creates a new list with the same top-level elements; nested objects are still shared.
What exception is raised when you try to access a dictionary key that doesn't exist?
Answer: KeyError
Accessing a missing dictionary key with bracket notation raises a `KeyError`.