Control Flow

If Statements

Use

if
statements to run code only when a condition is true.

Basic If Statement

age = 18

if age >= 18:
    print("You are an adult")

Important: Python uses indentation (4 spaces) to define code blocks!

If-Else

age = 15

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

If-Elif-Else

For multiple conditions:

score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"
Visualizer
$ python grades.py
Grade: B - Good job!

Interactive Visualization

python
Loading...
Output
Run execution to see output...