All lessons
๐Ÿ”€

If, elif, else

Make decisions in code

Reward
+60 XP

if statements

if runs a block when a condition is True. Indent with 4 spaces.

age = 20
if age >= 18:
    print("Adult")

elif and else

Use elif for additional branches and else for a fallback.

score = 72
if score >= 90:
    print("A")
elif score >= 70:
    print("B")
else:
    print("Try again")

Logical operators

Use and, or, not to combine conditions.

age = 20
has_id = True
if age >= 18 and has_id:
    print("Allowed")

Try it yourself

Run the example below. Edit it and see what changes.

main.py
Output
Press Run to execute (Ctrl/Cmd+Enter)

Common mistakes

  • โš Using = instead of == in the condition.
  • โš Forgetting the colon : at the end of the line.
  • โš Mixing tabs and spaces for indentation.
  • โš Writing 'else if' instead of 'elif'.