Python Tutorial · Control Flow · Lesson 19 of 42

Python If...Else

Make decisions with if, elif, else and conditional expressions.

if, elif, else

Example 1Try it yourself
temperature = 18
if temperature > 25:
    print("Hot")
elif temperature > 15:
    print("Mild")
else:
    print("Cold")

Output

Mild

Python checks each condition in order and runs the first block whose condition is true.

Conditional expression

For simple choices there's a one-line form: value_if_true if condition else value_if_false.

Example 2Try it yourself
age = 20
status = "adult" if age >= 18 else "minor"
print(status)

Output

adult

Combining conditions

Use and, or and not. Keep complex conditions readable by giving them a name:

Example 3Try it yourself
age = 34
has_ticket = True
is_vip = False
can_enter = (age >= 18 and has_ticket) or is_vip
if can_enter:
    print("Welcome!")

Output

Welcome!

Avoid deep nesting

Return early (or continue in loops) instead of nesting many if blocks — "guard clauses" keep code flat and readable.

Test yourself

Which keyword checks another condition after if?

  1. else if
  2. elseif
  3. elif
  4. case
Show answer

Answer: elif