โ† All Python Flashcard Decks

Operators and Expressions 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 Operators and Expressions flashcards as text
  1. What does the expression `lambda x: x * 2` create?

    Answer: An anonymous function that doubles its argument

    `lambda x: x * 2` creates an anonymous (lambda) function that takes one argument and returns it multiplied by 2.

  2. What is the result of `True + True + False` in Python?

    Answer: 2

    In Python, `True` equals `1` and `False` equals `0` in arithmetic contexts, so `1 + 1 + 0 = 2`.

  3. Which expression correctly checks if a variable `n` is between 1 and 10 (inclusive)?

    Answer: 1 <= n <= 10

    Python supports chained comparisons, so `1 <= n <= 10` is the correct and idiomatic way to check a range.

  4. What is the value of `~5` in Python?

    Answer: -6

    The bitwise NOT operator `~` inverts all bits; for integer `n`, `~n` equals `-(n + 1)`, so `~5 = -6`.

  5. What does `a, b = b, a` demonstrate in Python?

    Answer: Tuple unpacking for variable swapping

    Python evaluates the right side as a tuple `(b, a)` first, then unpacks it into `a` and `b`, effectively swapping them.

  6. What is the result of `2 ** 3 ** 2` in Python?

    Answer: 512

    The `**` operator is right-associative, so `2 ** 3 ** 2` is evaluated as `2 ** (3 ** 2) = 2 ** 9 = 512`.

  7. What does `5 | 3` evaluate to in Python?

    Answer: 7

    `5` is `101` and `3` is `011` in binary; bitwise OR gives `111`, which is `7`.