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 does the following code print? x = 10 if x > 5: print('A') elif x > 8: print('B') else: print('C')
Answer: A
The first condition `x > 5` is True, so 'A' is printed and the rest of the elif/else chain is skipped.
Which operator is used for the ternary (conditional) expression in Python?
Answer: if/else inline
Python uses `value_if_true if condition else value_if_false` as its ternary expression syntax.
What is the output of: `print('yes' if 0 else 'no')`?
Answer: no
0 is falsy in Python, so the else branch evaluates and 'no' is printed.
Which of the following is a valid Python conditional expression?
Answer: x = a if a > b else b
Python's conditional (ternary) expression syntax is `value_if_true if condition else value_if_false`.
What happens if no `elif` or `else` clause is present and the `if` condition is False?
Answer: Nothing happens; execution continues after the block
If the `if` condition is False and there are no other branches, Python simply skips the block and continues.
What is the result of the following? age = 20 status = 'adult' if age >= 18 else 'minor' print(status)
Answer: adult
Since `age >= 18` is True, the ternary expression evaluates to 'adult'.
In Python, how many `elif` clauses can follow a single `if` statement?
Answer: Unlimited
Python allows any number of `elif` clauses after an `if` statement.