What symbol MUST go at the end of an if header line?
A colon (:)
Which operator tests if two values are exactly equal?
==
Which keyword requires both conditions to be True?
and
Which keyword handles alternative conditions if the initial if is False?
elif
x = 10
if x > 15:
print("A")
else:
print("B")
"B"
How does Python know which statements belong inside an if block versus outside it?
Indentation (the indented block of code under the header)
What does the != operator check for?
"Not equal to"
Which keyword requires at least one condition to be True?
or
Which keyword acts as the catch-all fallback when no previous conditions match?
else
x = 8
if x > 5:
print("Alpha")
if x > 2:
print("Beta")
Both "Alpha" and "Beta" (on separate lines because these are two separate if statements).
What type of data (int, str, or bool) does an if statement evaluate to decide whether to run its block?
Boolean (True or False)
What does 10 >= 10 evaluate to? (True or False)
True
What does not True evaluate to?
False
How many elif blocks can you have under a single if statement?
As many as you want!
temp = 75
is_sunny = True
if temp > 80 or is_sunny:
if temp < 70:
print("Jacket")
else:
print("T-Shirt")
else:
print("Sweater")
"T-Shirt" (Outer if evaluates to True because is_sunny is True. Inner if fails, running the inner else).
What happens if an if condition evaluates to False and there are no elif or else blocks attached to it?
Python simply skips the entire block and moves on to the next unindented line of code.
If a = 5 and b = "5", what does a == b evaluate to in Python and why?
False because Python compares both value and data type (int vs str).
Evaluate this expression: (5 > 2 or 10 < 2) and not (3 == 3)
False (Left side is True, but not (True) makes the right side False; True and False = False).
What is the structural difference between running three separate if statements versus an if-elif-elif chain?
Separate if statements check every condition independently. An if-elif-elif chain stops as soon as it finds the first True condition.
val = 20
if val > 10:
val = val + 5
if val > 20:
val = val * 2
else:
val = val - 5
print(val)
50 (First if runs, updating val to 25. Then second if tests 25 > 20, which is True, making val equal 50).
Can you write an elif or else statement by itself without a preceding if statement? Why or why not?
No, elif and else depend on a parent if statement to evaluate prior conditions.
Given x = 10, rewrite x > 5 and x <= 15 as a chained comparison expression in Python syntax.
5 < x <= 15 (or 15 >= x > 5)
Given has_ticket = True and is_banned = False, does this evaluate to True or False?
has_ticket and not is_banned
True (not False becomes True, making it True and True).
How can a logic bug occur if you order your elif conditions from broadest to narrowest (e.g., checking x > 0 before x > 100)?
The broader condition (x > 0) triggers first for large numbers like 150, so the narrower check (x > 100) will never be reached.
a = True
b = False
if a or b:
if not b and a:
print("Option 1")
else:
print("Option 2")
else:
print("Option 3")
"Option 1"