← 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
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Backend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account