Python if Statement
Table of Contents
In the last lesson, we learned about Python Identity Operators. Now let’s use those boolean results to decide whether a block of code should run.
Let’s Talk About Decisions
Life is full of decisions.
If it is raining, take an umbrella.
If we are hungry, eat something.
If the light is red, stop.
We make these decisions all the time. And we do it without thinking.
Computers also need to make decisions. That is what the if statement is for.
It lets our program choose what to do based on a condition.
What Is the Python if Statement?
An if statement checks a condition. If the condition is true, it runs some code. If the condition is false, it skips that code.
Simple.
Let me show us.
age = 20
if age >= 18: print("You can vote")Output
You can voteHere is what happened.
- We set
ageto 20. - The
ifstatement checked: isage >= 18? Yes, 20 is greater than or equal to 18. So the condition is True. - Because it is True, Python ran the
printline.
Now if age was 15:
age = 15
if age >= 18: print("You can vote")Output
(nothing)Nothing printed. Because the condition was False. So Python skipped the print line.
That is the whole idea.
The Structure of a Python if Statement
Every if statement has three parts.
- The word
if - A condition
- A colon
: - Then indented code
Like this:
if condition: # do somethingThe condition is anything that gives True or False. Usually a comparison.
The colon : tells Python “here comes the code that belongs to this if.”
And the indented code is what runs if the condition is true.
Indentation Matters in Python if
Let’s remember, Python uses indentation to know which code belongs to the if.
if age >= 18: print("You can vote") print("Please go to the voting booth")Both print lines are indented. So both belong to the if. If the condition is true, both run.
If we write this:
if age >= 18: print("You can vote")print("This always runs")The second print is not indented. So it is outside the if. It runs no matter what.
That is important. Indentation is not just for looks. It changes what the code does.
Python if Statement: A Real-Life Example
Let’s check if a number is positive.
number = 10
if number > 0: print("The number is positive")Output
The number is positiveNow if the number was negative:
number = -5
if number > 0: print("The number is positive")Output
(nothing)Because -5 > 0 is False. So the code inside the if is skipped.
Multiple if Statements in Python
We can have multiple if statements. They are separate.
age = 20
if age >= 18: print("You can vote")
if age >= 16: print("You can drive")
if age >= 21: print("You can drink alcohol")Output
You can voteYou can driveWait. Why did “You can drink alcohol” not print? Because age >= 21 is False. 20 is not greater than or equal to 21.
So the first two if statements ran. The third did not.
Each if is checked independently. They do not affect each other.
Using if with Different Types in Python
We can use if with any type that gives a boolean.
name = "Alex"
if name: print("Name is not empty")Output
Name is not emptyBecause a non-empty string is truthy. So the condition is True.
name = ""
if name: print("Name is not empty")Output
(nothing)Because an empty string is falsy. So the condition is False.
This is a common pattern. Checking if a string is not empty.
Python if with input()
Let’s ask the user for their age. And check if they can vote.
age = int(input("Enter your age: "))
if age >= 18: print("You can vote")If the user enters 20, the output is:
You can voteIf the user enters 15, nothing prints.
But that is not very friendly. If they cannot vote, we should tell them. That is what else is for.
We will learn about else in the next article. But for now, let’s just know that if alone only handles the true case.
Common Mistakes with Python if Statement
Mistake 1: Missing the Colon
Here is a mistake that every beginner makes.
age = 20
if age >= 18 print("You can vote")What is missing? The colon : after the condition.
Python will give us an error:
SyntaxError: expected ':'So what do we do? We add the colon.
if age >= 18: print("You can vote")Mistake 2: Missing Indentation
age = 20
if age >= 18:print("You can vote")The print line is not indented. So Python does not know it belongs to the if.
So what do we do? We indent it.
if age >= 18: print("You can vote")Let’s always remember: colon at the end, indentation after.
Nested if Statements in Python
We can put an if inside another if. This is called nesting.
age = 20has_id = True
if age >= 18: if has_id: print("You can enter")Output
You can enterFirst, check if age is >= 18. If yes, then check if has_id is True. If both are true, print.
We will learn more about nested conditions later. For now, let’s just know it is possible.
Next, we will learn about the Python if-else Statement and give our program a second path when a condition is false.