Conditions help Python choose a path.
A condition is an expression that Python can evaluate as True or False. In Operators, you learned that comparisons such as age >= 18 produce a Boolean result. Now if uses that result to choose which block of code runs: condition → True/False → choose a branch → run that block.
age = 20
if age >= 18:
print("You are an adult")Python first evaluates age >= 18 and gets True. Because the condition is True, Python enters the indented block and executes print(...). If the condition were False, Python would skip that block.
condition → True/False → choose a branch → run that blockThe pattern if condition: starts a decision. if introduces the question, the condition produces a Boolean result, the colon announces a block, and indentation shows which code belongs to that block.
Indentation matters: Indentation tells Python which statements belong to a branch. It is not only visual formatting: an if statement without an indented block, such as if age >= 18:
print("Access granted"), raises an IndentationError.
temperature = 12
if temperature > 20:
print("It's warm")
else:
print("It's cold")An else block is the other path for the same question. Because temperature > 20 is False, Python skips the if block and runs else. Exactly one of these two branches runs.
if age >= 18:
print("Adult")
print("Access granted")
print("Program continues")The first two print() lines are indented, so they belong to the if block. print("Program continues") is not indented, so it runs after the decision whether or not the if block ran.
score = 75
if score >= 90:
print("Excellent")
elif score >= 60:
print("Passed")
else:
print("Try again")Python checks this if/elif/else chain from top to bottom. score >= 90 is False, so Python checks score >= 60. That is True, so it prints "Passed" and skips the remaining branch. elif is part of the same chain, not a separate if: the first True branch wins.
score = 95
# Too broad first
if score >= 60:
print("Passed")
elif score >= 90:
print("Excellent")
# More specific first
if score >= 90:
print("Excellent")
elif score >= 60:
print("Passed")In the first chain, score >= 60 is already True, so Python prints "Passed" and never checks the elif. Python does not search for a better branch. When conditions overlap, put the more specific or stronger condition before the broader one.
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome!")
else:
print("Access denied")age >= 18 produces a Boolean result, while has_ticket is already a Boolean value. and combines those results, and if uses the final True or False to choose a branch. This is the Operators lesson applied to a decision.
age = 16
print("Checking age...")
if age >= 18:
print("Adult")
else:
print("Minor")
print("Done")Before you run this program, ask: Which lines always run? Which condition is checked? Which branch runs, and which branch is skipped? What is the complete output order? Here "Checking age..." runs first, the condition is False, so "Minor" runs, and "Done" runs last. This is the order in which the program follows its code.
