The if statement

An if statement runs a block of code only when a condition is true. The block is everything indented beneath the colon:

temperature = 31
if temperature > 30:
    print("It is hot today.")
    print("Drink water.")
print("This line always runs.")
It is hot today. Drink water. This line always runs.

Indentation is not decoration in Python — it is the syntax. Use four spaces (the app's editor inserts them automatically) and be consistent. Mixing tabs and spaces raises IndentationError.

else and elif

score = int(input("Score: "))
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"
print("Grade:", grade)
Score: 84 Grade: B

Python checks each condition from the top and runs the first block that is true, then skips the rest. Order matters: if you tested score >= 70 first, a score of 95 would get a C.

Combining conditions

age = 16
with_adult = True

if age >= 18 or (age >= 13 and with_adult):
    print("Admitted")
else:
    print("Not admitted")

username = ""
if not username:
    print("Username is required")

fruit = "kiwi"
if fruit in ("apple", "kiwi", "pear"):
    print("We stock that")
Admitted Username is required We stock that

Nested if statements

logged_in = True
is_admin = False

if logged_in:
    if is_admin:
        print("Show admin panel")
    else:
        print("Show user dashboard")
else:
    print("Show login page")
Show user dashboard

Nesting works but gets hard to read past two levels. Often you can flatten it with and, or by returning early from a function (see functions).

The conditional expression (ternary)

n = 7
parity = "even" if n % 2 == 0 else "odd"
print(n, "is", parity)

items = 1
print(f"{items} item{'s' if items != 1 else ''}")
7 is odd 1 item

Use it for short, single-value choices. If either branch needs more than one expression, write a full if/else.

match statement (Python 3.10+)

For comparing one value against many patterns, match is cleaner than a long elif chain:

command = "stop"
match command:
    case "start" | "go":
        print("Starting")
    case "stop":
        print("Stopping")
    case _:
        print("Unknown command")
Stopping

match can also destructure lists, tuples and dictionaries, which becomes useful once you work with structured data.

Write your own grade calculator in the app — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
Get it free

Practice exercise

Write a program that asks for a year and prints whether it is a leap year. A year is a leap year if it is divisible by 4, except years divisible by 100, unless they are also divisible by 400.

year = int(input("Year: "))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")
Year: 2024 2024 is a leap year

The Coding Python app has a challenge like this that checks your answer automatically. Next up: loops.

🚦
Practise this: Gatekeeper

Write the rule that lets exactly the right ones through — 6 levels that run real Python in your browser.

Play free

Frequently asked questions

What is the difference between elif and else in Python?

elif tests another condition and runs only if it is true; else has no condition and runs when every previous test failed. You can have many elif branches but only one else.

Why do I get IndentationError?

The lines inside an if block must be indented by the same amount, and the if line must end with a colon. Mixing tabs and spaces is the usual cause; use four spaces everywhere.

Does Python have a switch statement?

Python 3.10 added match/case, which covers what switch does in other languages and also supports pattern matching on structure.

Can I write an if statement on one line?

Yes, for simple cases: x = 'yes' if cond else 'no' is a conditional expression. Multi-statement blocks should use the normal indented form.