← Python tutorial

Python

Conditionals

if runs a block only when its condition is true. elif ("else if") checks another condition if the first was false, and else catches everything else.

Example: a time-of-day greeting

hour = 14

if hour < 12:
    greeting = "Good morning"
elif hour < 18:
    greeting = "Good afternoon"
else:
    greeting = "Good evening"

print(greeting)
Good afternoon

Python uses indentation — not curly braces — to mark which lines belong to which block. This isn't optional style here the way it is in many languages: inconsistent indentation is an actual syntax error in Python, not just something a linter complains about. That constraint is deliberate — it makes it structurally impossible to write Python code where the indentation lies about what the code actually does.

Example
hour = 14

if hour < 12:
    greeting = "Good morning"
elif hour < 18:
    greeting = "Good afternoon"
else:
    greeting = "Good evening"

print(greeting)
Output
Good afternoon