Python17 min read

Python If Statements

Learn conditions, comparisons, and clean decision-making using if, elif, and else with practical examples and outputs.

Emily Davis
August 7, 2025
6.2k279

If statements let your program choose different paths based on data. This is the foundation of “logic” in programming.

The core idea

  • A condition evaluates to either True or False
  • If True, Python runs the indented block

Basic if

age = 18

if age >= 18:
    print("You can vote.")

Expected output:

You can vote.

if / else

temperature = 75

if temperature > 80:
    print("It's hot in Phoenix.")
else:
    print("Nice weather in Seattle.")

Expected output:

Nice weather in Seattle.

if / elif / else

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
else:
    print("Grade: F")

Expected output:

Grade: B

Comparisons you must know

x = 10
print(x == 10)
print(x != 5)
print(x > 5)
print(x <= 10)

Expected output:

True
True
True
True

Logical operators: and / or / not

age = 25
has_license = True

if age >= 18 and has_license:
    print("You can drive.")

Expected output:

You can drive.

Graph: decision flow

flowchart TD
  A[Check condition] --> B{True?}
  B -->|Yes| C[Run block]
  B -->|No| D[Skip or next condition]

In the next lesson, you will learn loops so you can repeat actions without copying and pasting code.

#Python#Beginner#Conditionals