Python if else Statement
Table of Contents
In the last lesson, we learned about the Python if Statement. Now let’s add another path for when the condition is false.
What If the Condition Is False?
In the last article, we learned about if. It runs code when a condition is true.
But what if the condition is false? What if we want to do something else?
That is where else comes in.
else lets us say: “If the condition is true, do this. Otherwise, do that.”
It is like a fork in the road. One path if yes. Another path if no.
The Structure of if-else in Python
if condition: # do this if condition is trueelse: # do this if condition is falseNotice the else line. It has no condition. It just says “otherwise.”
And it also has a colon : and indented code.
A Simple Python if-else Example
Let’s check if someone can vote.
age = 15
if age >= 18: print("You can vote")else: print("You cannot vote yet")Output
You cannot vote yetBecause 15 >= 18 is False. So the if block is skipped. And the else block runs.
Now if age was 20:
age = 20
if age >= 18: print("You can vote")else: print("You cannot vote yet")Output
You can voteBecause 20 >= 18 is True. So the if block runs. And the else block is skipped.
Only one of the two blocks will ever run. Never both. That is the rule.
Python if-else: A Real-Life Example
Let’s check if a number is even or odd.
number = 7
if number % 2 == 0: print("The number is even")else: print("The number is odd")Output
The number is oddBecause 7 divided by 2 gives a remainder of 1. So 7 % 2 == 0 is False. The else block runs.
If the number was 10:
number = 10
if number % 2 == 0: print("The number is even")else: print("The number is odd")Output
The number is evenBecause 10 divided by 2 gives remainder 0. So the condition is True. The if block runs.
Using if-else with input()
Let’s ask the user for their age and tell them if they can vote.
age = int(input("Enter your age: "))
if age >= 18: print("You can vote")else: print("You cannot vote yet")If the user enters 20, output is You can vote.
If the user enters 15, output is You cannot vote yet.
Now the program always gives a response. It is friendlier.
Python if-else: Another Example — Password Check
password = input("Enter password: ")
if password == "secret123": print("Access granted")else: print("Access denied")This is a simple password checker. If the password matches, grant access. Otherwise, deny.
Multiple Conditions? Not Here.
With just if and else, we only have two paths. One for true, one for false.
But what if we have more than two possibilities?
For example, grades: A, B, C, D, F. That is five possibilities.
We could nest if-else inside if-else. But that gets messy.
That is why Python has elif. We will learn about that in the next article.
For now, let’s just remember: if-else is for two choices.
Common Mistakes with Python if-else
Mistake 1: Forgetting the Colon
if age >= 18: print("You can vote")else print("You cannot vote")Error! Missing colon after else.
So what do we do? We add the colon.
else:Mistake 2: Forgetting Indentation
if age >= 18:print("You can vote")else:print("You cannot vote")Error! The print lines need indentation.
So what do we do? We indent them.
if age >= 18: print("You can vote")else: print("You cannot vote")Mistake 3: Using else if Instead of elif
In some languages, we write else if. In Python, it is elif. But we will cover that next.
For now, let’s just know that else does not take a condition.
Mistake 4: Putting Code After else That Should Be Outside
if age >= 18: print("You can vote")else: print("You cannot vote")print("This always runs")The last print is not indented. So it runs no matter what. That is correct if we want it to always run.
But if we wanted it only in the else case, we need to indent it.
A Complete Python if-else Example
Let’s build a simple login system.
username = input("Enter username: ")password = input("Enter password: ")
if username == "admin" and password == "1234": print("Login successful")else: print("Invalid username or password")This checks both username and password. If both match, success. Otherwise, failure.
Next, we will learn about the Python elif Statement and handle more than two possible paths.