Lesson 6: Conditional Logic (if/elif/else)

How Python makes decisions and controls program flow

Why Conditional Logic Matters

Conditional statements allow your program to make decisions based on conditions. They control what code runs depending on whether something is true or false.

The if Statement

The simplest form of a condition:

age = 18

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

if/else Structure

Use else when you want an alternative action:

temperature = 10

if temperature > 20:
    print("It's warm outside.")
else:
    print("It's cold outside.")

Using elif for Multiple Conditions

elif lets you check additional conditions:

score = 75

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

Comparison Operators

Logical Operators

Combine conditions using:

age = 25
has_id = True

if age >= 18 and has_id:
    print("Access granted.")

Nesting Conditions

You can place conditions inside other conditions:

age = 16
parent_present = True

if age < 18:
    if parent_present:
        print("Allowed with supervision.")
    else:
        print("Not allowed.")

Truthy and Falsy Values

Python treats some values as True or False automatically:

if "":
    print("This won't run.")
if "hello":
    print("This will run.")

Next Steps

Now that you understand conditional logic, you’re ready to learn how Python repeats actions using loops in Lesson 7: Loops (for, while).

← Back to Lesson Index