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 of: x = None if x: print('truthy') else: print('falsy')
Answer: falsy
None is falsy in Python, so the else branch executes and prints 'falsy'.
Which of the following correctly checks if a variable `n` is between 1 and 10 inclusive?
Answer: if 1 <= n <= 10:
Python supports chained comparisons like `1 <= n <= 10`, which is the most Pythonic way.
What does `pass` do inside an `if` block?
Answer: Does nothing; acts as a placeholder
`pass` is a no-op statement used as a syntactic placeholder when a block is required but no action is needed.
What is the output? for i in range(3): if i == 1: pass else: print(i)
Answer: 0 2
When i==1 the `pass` does nothing and no print occurs, so only 0 and 2 are printed.
Which of the following correctly uses `not` in an if statement?
Answer: if not x == 5:
`not` is Python's logical negation operator and `not x == 5` is valid syntax.
What is printed? val = '' if val: print('has value') elif val is None: print('is None') else: print('empty')
Answer: empty
An empty string is falsy but not None, so neither the if nor the elif match, and 'empty' is printed.
In Python, `if x is None:` differs from `if x == None:` because:
Answer: `is` checks identity, while `==` checks equality
`is` tests object identity (same object in memory), while `==` tests value equality; PEP 8 recommends `is None`.