Control Flow: Conditional Statements 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 Control Flow: Conditional Statements flashcards as text
What is the output? x = [1, 2, 3] if x: print('not empty') else: print('empty')
Answer: not empty
A non-empty list is truthy, so the if branch runs and prints 'not empty'.
Which of the following will cause a SyntaxError?
Answer: if x > 0 print(x)
Python requires a colon `:` at the end of an `if` statement; omitting it causes a SyntaxError.
What is the output of this code? color = 'blue' if color == 'red': print(1) elif color == 'green': print(2) elif color == 'blue': print(3) else: print(4)
Answer: 3
The third condition `color == 'blue'` is True, so 3 is printed.
What is the output? x = 15 if x % 2 == 0: print('even') if x % 3 == 0: print('divisible by 3') if x % 5 == 0: print('divisible by 5')
Answer: divisible by 3\ndivisible by 5
These are three independent if statements, not an if/elif chain; 15 is divisible by both 3 and 5, so both print.
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.
What is the output of this walrus-operator expression (Python 3.8+)? data = [1, 2, 3] if n := len(data): print(n)
Answer: 3
The walrus operator `:=` assigns `len(data)` to `n` and evaluates it as the condition; 3 is truthy so n (which is 3) is printed.
What does `elif` stand for in Python?
Answer: else if
`elif` is shorthand for 'else if', used to chain multiple conditional checks.