← All Python Flashcard Decks

Mixed Deck — All Python Topics Flashcards

99 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 20 Mixed Deck — All Python Topics flashcards as text
  1. What is the result of `divmod(17, 5)`?

    Answer: (3, 2)

    `divmod(17, 5)` returns a tuple of `(quotient, remainder)`, which is `(3, 2)` since `17 = 5*3 + 2`.

  2. Which of the following scenarios best demonstrates the concept of polymorphism in Python?

    Answer: A function `render_shape(shape)` that can correctly draw a `shape` object whether it is a `Circle` or a `Square`, because both classes have a `.draw()` method.

    Polymorphism (from Greek, meaning "many forms") is the ability of an object to take on many forms. In programming, it means that a single interface (like the `render_shape` function) can be used for objects of different types. The function can operate on different shape objects without needing to know their specific type, as long as they adhere to the common interface of having a `.draw()` method.

  3. What is the third argument to file.seek(offset, whence)?

    Answer: The reference position: 0=start, 1=current, 2=end

    The whence argument specifies the reference point: 0 for start of file, 1 for current position, and 2 for end of file.

  4. Which method removes and returns the last element of a Python list?

    Answer: pop()

    `list.pop()` removes and returns the last element by default, or an element at a given index.

  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. What does the following code output? print(bool(0), bool(3.14159), bool(-3), bool(1.0+1j))

    Answer: False True True True

    The `bool()` function converts a value to its boolean equivalent. In Python, zero (0), empty sequences/collections, and `None` are considered "falsy". All other numbers (including non-zero integers, floats, and complex numbers) are considered "truthy". Therefore, `bool(0)` is `False`, while `bool(3.14159)`, `bool(-3)`, and `bool(1.0+1j)` are all `True`.

  7. What is a mixin in Python OOP?

    Answer: A small class designed to add specific behavior via multiple inheritance

    Mixins are narrowly focused classes added to a class hierarchy via multiple inheritance to inject reusable behavior without deep coupling.

  8. How can you write a one-line if statement in Python?

    Answer: if cond: stmt

    Python allows `if condition: statement` on a single line when there is only one statement in the block.

  9. What does the format string `'{0} {1}'.format('Hello', 'World')` produce?

    Answer: 'Hello World'

    Positional placeholders {0} and {1} are replaced with the first and second format arguments.

  10. What is a 'free variable' in the context of Python closures?

    Answer: A variable used in a function but defined in an enclosing outer scope

    A free variable is a variable referenced inside a function but not defined in that function's local scope — it is bound in an enclosing scope and captured by the closure.

  11. What does the bitwise XOR operator `^` return for `6 ^ 3`?

    Answer: 5

    `6` is `110` and `3` is `011` in binary; XOR gives `101`, which is `5`.

  12. Consider the following code snippet. What will be the final content of `inventory`? ```python inventory = {'apples': 10, 'oranges': 5} updates = {'oranges': 15, 'grapes': 20} inventory.update(updates) ```

    Answer: {'apples': 10, 'oranges': 15, 'grapes': 20}

    The `.update()` method merges the keys and values from one dictionary into another. If a key already exists in the original dictionary, its value is updated with the value from the new dictionary. If a key does not exist, the new key-value pair is added.

  13. What does the walrus operator `:=` do in Python?

    Answer: Assigns a value and returns it in the same expression

    The walrus operator `:=` (assignment expression) assigns a value to a variable and simultaneously returns that value within an expression.

  14. Which exception is raised when you try to access a list index that is out of range?

    Answer: IndexError

    `IndexError` is raised when a sequence subscript (index) is out of the valid range.

  15. What does the `global` keyword do inside a Python function?

    Answer: Declares that a variable name refers to the module-level global variable

    `global x` inside a function tells Python to use and potentially modify the `x` defined at the module level rather than creating a local one.

  16. What is the result of `[str(x) for x in range(3)]`?

    Answer: ['0', '1', '2']

    str() converts each integer to its string representation, producing ['0', '1', '2'].

  17. What is the output of `bool([])` in Python?

    Answer: False

    An empty list is falsy in Python, so `bool([])` returns `False`.

  18. Consider the following code. What will be the final output printed to the console? ```python def check_value(): try: print("Start") raise ValueError print("After Raise") except ValueError: print("Caught") return "From Except" finally: print("Cleanup") print("End") print(check_value()) ```

    Answer: Start Caught Cleanup From Except

    The `try` block starts and prints "Start". A `ValueError` is immediately raised, so "After Raise" is skipped. The `except ValueError` block is executed, printing "Caught". It then prepares to return "From Except". However, the `finally` block must execute before the function officially returns, so "Cleanup" is printed. After the `finally` block completes, the function returns the value from the `except` block, which is then printed. "End" is never reached because the return statement exits the function.

  19. How do you make a class attribute read-only for instances?

    Answer: Use `@property` with only a getter and no setter

    A `@property` with only a getter raises `AttributeError` on assignment, effectively making the attribute read-only from outside.

  20. What is the result of `type(lambda: None)`?

    Answer:

    Lambda expressions create anonymous functions; their type is `function`.