An if statement makes a program take different paths based on a condition. It’s the most fundamental piece of control flow in any language.

The basic if

age: int = 20

if age >= 18:
    print("You can vote.")
  • The line if age >= 18: ends with a colon.
  • The body — what runs when the condition is True — is indented below it.
  • Python uses indentation (usually four spaces) instead of { } to mark code blocks.

If the condition is False, the indented block is skipped.

if/else

To run different code when the condition is false, add an else branch:

age: int = 16

if age >= 18:
    print("You can vote.")
else:
    print("Too young to vote.")

if/elif/else

For more than two paths, use elif (short for “else if”). You can have as many elif branches as you need:

score: int = 75

if score >= 90:
    grade: str = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 50:
    grade = "D"
else:
    grade = "F"

print(grade)   # C

Python checks each condition top to bottom. The first one that’s True runs, and the rest are skipped. If none match, the else runs (if there is one).

Indentation is the syntax

Most languages use { } to mark blocks. Python uses indentation. This is non-negotiable:

# correct — 4-space indent
if True:
    print("indented")

# wrong — no indent
if True:
print("not indented")   # IndentationError

Pick four spaces and stick to it. Don’t mix tabs and spaces — Python will complain. Your editor’s format-on-save handles this automatically.

Nested conditions

You can put an if inside another if:

age: int = 20
has_id: bool = True

if age >= 18:
    if has_id:
        print("Welcome.")
    else:
        print("Please show ID.")
else:
    print("Too young.")

This works, but deeply nested code becomes hard to read. Usually it’s clearer to combine conditions with and:

if age >= 18 and has_id:
    print("Welcome.")
elif age >= 18:
    print("Please show ID.")
else:
    print("Too young.")

Truthy and falsy in conditions

Recall from Section 2: any value can be used in an if, not just booleans. Falsy values include 0, "", None, [], {}. Everything else is truthy.

name: str = input("Name: ")

if name:
    print(f"Hello, {name}")
else:
    print("You didn't type anything.")

Here if name: is the idiomatic way to say “if the string isn’t empty”.

Similarly, to check if a list has items:

results: list[int] = get_results()

if results:
    print(f"Found {len(results)} results")
else:
    print("No results")

Don’t write if len(results) > 0:if results: is shorter and the standard Python style.

pass — when the body is empty

Sometimes you write an if block but haven’t decided what goes inside yet. Python doesn’t allow an empty body, so use pass:

if score >= 90:
    pass   # TODO: handle top performers
else:
    print(f"Score: {score}")

pass is a statement that does nothing. It’s a placeholder.

A small example

A program that classifies a temperature:

temp_c: float = float(input("Temperature in °C: "))

if temp_c < 0:
    description: str = "Freezing"
elif temp_c < 15:
    description = "Cold"
elif temp_c < 25:
    description = "Comfortable"
elif temp_c < 35:
    description = "Hot"
else:
    description = "Very hot"

print(f"{temp_c}°C — {description}")

What’s next

You can make decisions. Next, the first kind of loop — for loops, for processing each item in a collection.

Toggle theme (T)