Control flow · 10 sample answers

Python conditionals: practice questions with answers.

Conditionals look simple until two branches overlap or a truthy value surprises you. These questions check the order of checks, the difference between assignment and comparison, and the small indentation decisions that change behaviour.

43 questions in the full bank · 16 choice · 8 fill · 19 output

What this topic tests

  • Order if and elif branches so the broadest condition is not first.
  • Predict truthiness for empty collections, zero, and None.
  • Use comparison chains and boolean operators without ambiguity.

Common mistakes

  • Using = when you meant ==.
  • Putting a broad if before a more specific elif, which makes the elif unreachable.
  • Misreading indentation and assuming a statement belongs to the branch above it.
Sample questions

Sample questions

10

Ten representative questions from the full bank. Try each one before opening the answer.

  1. Multiple choiceq0194

    1. Ming only makes milk tea when there is both milk and tea. What is True and False?

    1. True
    2. False
    3. raises an error
    4. None
    Show answer

    Answer

    B. False

    Why: and returns False as soon as one side is false and does not evaluate the rest. Both conditions must hold, so the result is False.

  2. Multiple choiceq0201

    2. 🧋 Ming only makes milk tea when there is milk and tea. What is the value of True and 0?

    1. True
    2. 0
    3. False
    4. 1
    Show answer

    Answer

    B. 0

    Why: and returns the first falsy operand, and 0 is falsy, so the result is 0 rather than False. Truthiness and identity are different things.

  3. Multiple choiceq0207

    3. 🧋 Which option is equivalent to the conditional expression a if cond else b?

    1. an if/else block that chooses a or b
    2. cond ? a : b
    3. a when cond otherwise b
    4. (cond, a, b)
    Show answer

    Answer

    A. an if/else block that chooses a or b

    Why: The conditional expression is shorthand for an if/else that picks a or b; the ? : form belongs to C-family languages, not Python.

  4. Multiple choiceq0217

    4. If the condition is `if 0:`, which branch runs?

    1. The else branch
    2. The if branch
    3. An error occurs
    4. Both branches
    Show answer

    Answer

    A. The else branch

    Why: if 0 tests truthiness, and 0 is false, so the if body is skipped and the else branch runs; nothing raises an error here. An empty list or empty string behaves the same way.

  5. Fill in the blankq0196

    5. Ming checks whether “boba” is in the topping list ["boba", "coconut jelly", "pudding"]: "boba" ___ ["boba", "coconut jelly", "pudding"]

    Show answer

    Answer

    in

    Why: in tests membership and returns a bool; boba is one of the three toppings, so the check is True. not in is its negation. Membership always answers with a bool.

  6. Fill in the blankq0204

    6. 🧋 Ming checks the milk-and-tea conditions. Converting the result of `1 and 2` to bool gives ___.

    Show answer

    Answer

    True

    Why: 1 and 2 returns 2 because both operands are truthy, and bool(2) is True. The value and its truthiness are separate questions.

  7. Fill in the blankq0212

    7. 🧋 Ming checks whether a drink is on the menu. Test whether the key 'age' is in dictionary d: 'age' ___ d

    Show answer

    Answer

    in

    Why: For a dict, in checks keys rather than values, so 'age' in d asks whether that key exists; use d.values() to test values instead.

  8. Output predictionq0195

    8. How is 0 treated in a conditional expression, and what is printed?

    x = 0
    print("yes" if x else "no")
    Show answer

    Answer

    no

    Why: Truthiness treats 0, an empty string, an empty list and None as false, so the else value is chosen. Only a truthy x would print yes.

  9. Output predictionq0220

    9. What does `score(-5, 0)` return?

    from math import sqrt
    
    # X, and Y variable names against [pylint]: C0104, but is the same as the stub, advise not to change this.
    def score(x, y):
        dart_location = sqrt(x * x + y * y)
    
        if dart_location <= 1.0:
            return 10
        elif dart_location <= 5.0:
            return 5
        elif dart_location <= 10.0:
            return 1
        else:
            return 0
    
    print(score(-5, 0))
    Show answer

    Answer

    5

    Why: sqrt(25 + 0) = 5.0, above 1.0 but within 5.0, so the second branch returns 5. The boundary is inclusive because the test is <=.

  10. Output predictionq0226

    10. What does `equilateral([0.5, 0.5, 0.5])` return?

    def valid(sides):
        return sum(sorted(sides)[:2]) >= sorted(sides)[2] and all(
            side > 0 for side in sides
        )
    
    
    def equilateral(sides):
        return valid(sides) and all(sides[0] == side for side in sides)
    
    
    def isosceles(sides):
        return valid(sides) and any(
            side_1 == side_2 for side_1, side_2 in zip(sorted(sides), sorted(sides)[1:])
        )
    
    
    def scalene(sides):
        return valid(sides) and not isosceles(sides)
    
    print(equilateral([0.5, 0.5, 0.5]))
    Show answer

    Answer

    True

    Why: Floats work the same way: 0.5 + 0.5 >= 0.5 and all sides are equal, so equilateral returns True. Nothing restricts the check to integers.

Common questions

Is elif the same as else followed by if?

Logically it is similar, but elif keeps the branches mutually exclusive and avoids extra indentation.

What values are false?

False, None, numeric zero, and empty strings or collections are false. Most other objects are true.

Keep the streak going.

The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.

Open full practice