← All Python Flashcard Decks

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
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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`.