Conditions help Python choose a path.
A condition is an expression that is either <code>True</code> or <code>False</code>. Python uses that result to decide which block of code to run.
age = 20
if age >= 18:
print("You are an adult")The indented print() line runs only when age >= 18 is True. The colon starts the block, and indentation tells Python which code belongs inside it.
You are an adultWith if and else, Python has two possible paths. else is the alternative when the if condition is False.
Indentation matters: Python uses indentation to define a block. 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")Because temperature > 20 is False, Python skips the first block and runs the else block.
score = 75
if score >= 90:
print("Excellent")
elif score >= 60:
print("Passed")
else:
print("Try again")Python checks an if/elif/else chain from top to bottom and runs the first matching branch. elif is part of the same chain, not a separate if.
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome!")
else:
print("Access denied")Use and when both conditions must be true, or when at least one can be true, and not to reverse a true/false value. These are the Operators ideas applied to a decision.
