Lesson 4: Python Conditional Statements

⏱️ 3 min read

In real life, you make decisions based on conditions every day. You might think, “If it is raining, I will take an umbrella. Otherwise, I will wear sunglasses.”

Python handles logic the same way. Conditional statements allow your code to examine data, evaluate a situation, and decide what action to take next. Instead of running every single line of code from top to bottom, your program can skip sections or choose different paths based on the rules you set.

Here is how you control the flow of your Python programs using if, else, and elif.

1. The if Statement (The Basic Check)

The if statement is the simplest form of decision-making. It checks a condition (using the operators we discussed in the last lesson). If that condition evaluates to True, Python runs the code block underneath it. If it evaluates to False, Python ignores that code entirely.

The Syntax: Notice the colon : at the end of the if statement, and how the next line is indented. This indentation is mandatory in Python.

temperature = 35

# The condition: Is the temperature greater than 30?
if temperature > 30:
    print("It is a hot day. Drink plenty of water!")

2. The else Statement (The Fallback Plan)

What if the condition is not met? That is where else comes in. The else statement catches everything that the if statement misses. It does not need a condition of its own—it simply means “otherwise.”

password = "wrong_password"

if password == "secret123":
    print("Access Granted.")
else:
    print("Access Denied. Please try again.")

3. The elif Statement (Multiple Choices)

Life is rarely just black and white; sometimes you have more than two options. In Python, elif stands for “else if.” It allows you to chain multiple conditions together.

Python will check them one by one from top to bottom. As soon as it finds a True condition, it runs that code and skips the rest.

score = 85

if score >= 90:
    print("You got an A!")
elif score >= 80:
    print("You got a B!")
elif score >= 70:
    print("You got a C!")
else:
    print("You need to study more.")

The Golden Rule of Python: Indentation

In many programming languages, you use curly brackets {} to group code together. Python is different. It relies entirely on indentation (spaces or tabs at the start of a line) to know which code belongs to which if statement.

Incorrect (This will cause an error):

if True:
print("This will crash.")

Correct:

if True:
    print("This works perfectly.")

Putting It All Together

Let’s look at a practical example using everything at once. Imagine you are writing software for a movie theater that determines ticket prices based on a customer’s age.

customer_age = 25
is_student = True

print("Checking ticket price...")

if customer_age < 12:
    print("Child ticket: $8")
elif customer_age >= 65:
    print("Senior ticket: $10")
elif is_student:
    # This condition only gets checked if the person is between 12 and 64
    print("Student discount ticket: $12")
else:
    print("Standard adult ticket: $15")

print("Enjoy the movie!")

Ready to continue?

Move on to the next lesson to keep learning.

Continue: Lesson 5: Python Loops

Tutorial: Python