Conditional statements let your program make decisions — execute one block of code or another based on whether a condition is True or False. This is where Python starts thinking!
if StatementExecutes a code block only if the condition is True. If the condition is False, the block is skipped entirely.
if condition: code block # runs only when condition is True
if must be indented. If you don't indent, Python will give an IndentationError.
marks = 80 if marks >= 50: print("You passed!") print("Program continues...") # always runs
From your file (control-flow.py), notice the difference between these two code sets:
# Code set 1 — if without else: if False: print("Statement 1") # skipped print("Statement 2") # always runs — outside if block # Code set 2 — if with else: if False: print("Statement 1") # skipped else: print("Statement 2") # runs because if was False
if-else StatementRuns one block when the condition is True, and a different block when it's False. One of them always runs.
if condition: code block # runs if True else: code block # runs if False
age = int(input("Enter your age: ")) if age >= 18: print("You are an adult.") else: print("You are a minor.")
if-elif-else StatementChecks multiple conditions in sequence. The first condition that is True wins — its block runs, and the rest are skipped. The else at the end is the fallback when no condition is True.
if condition_1: code block elif condition_2: code block else: code block
elif is short for "else if". You can have as many elif blocks as you need. Only the first matching block runs.
marks = int(input("Enter marks: ")) if marks >= 90: print("Grade: A") elif marks >= 75: print("Grade: B") elif marks >= 60: print("Grade: C") elif marks >= 50: print("Grade: D") else: print("Grade: F — Failed")
From your file example:
if True: print("Statement 1") # runs — first True condition elif True: print("Statement 2") # skipped — first block already ran else: print("Statement 3") # skipped
A condition inside another condition. Use this when you need to check a second condition only after the first one passes.
if condition_1: if condition_2: # only checked if condition_1 is True code block else: code block else: code block
# From control-flow.py teacher example: if True: if True: print("Statement 1") # runs else: print("Statement 2") else: print("Statement 3")
Real-world example:
age = int(input("Enter age: ")) has_ticket = input("Have a ticket? (yes/no): ") if age >= 18: if has_ticket == "yes": print("Welcome! Enjoy the event.") else: print("Adult, but no ticket.") else: print("You must be 18+ to enter.")
if statements (3+ levels) become hard to read. Often you can simplify using and in a single condition.
"Pass" if marks ≥ 50, otherwise print "Fail".
marks = int(input("Enter your marks: ")) if marks >= 50: print("Pass") else: print("Fail")
"Positive", "Negative", or "Zero" using if-elif-else.
num = int(input("Enter a number: ")) if num > 0: print("Positive") elif num < 0: print("Negative") else: print("Zero")
if-elif-else.
marks = int(input("Enter marks (0-100): ")) if marks >= 90: print("Grade: A") elif marks >= 75: print("Grade: B") elif marks >= 60: print("Grade: C") elif marks >= 50: print("Grade: D") else: print("Grade: F")
age = int(input("Enter your age: ")) has_voter_id = input("Do you have a voter ID? (yes/no): ") if age >= 18: if has_voter_id == "yes": print("You can vote!") else: print("You need a voter ID to vote.") else: print("You must be 18+ to vote.")
x = 15
if x > 20:
print("A")
elif x > 10:
print("B")
elif x > 5:
print("C")
else:
print("D")
# Output: B
# Why: x=15 → 15>20? No. → 15>10? YES → print "B" → stop.
# Even though 15>5 is also True, elif stops at first match.
if statement?if-elif-else chain, how many blocks can run?if False: print("A") \n print("B") (B is not indented)elif stand for?if True: print("1") elif True: print("2") else: print("3")if (inside another if) checked?if-else, when condition is False, what happens?