- Home
- /
- Python if-else Statement
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python if-else Statement
if-else Statement
There are moments in programming when we want the code to do one thing if a condition is true, and something else if it’s false. That’s exactly where the if-else
statement comes in.
It gives us two paths:
One path when the condition is
True
Another when it’s
False
Basic Structure
if condition:
# runs if condition is True
else:
# runs if condition is False
Example:
Let’s check whether apples can be added to the cart based on the user’s budget:
apple_price = 210
user_budget = 200
if apple_price <= user_budget:
print("Add 1kg apples to the shopping cart.")
else:
print("Don’t add apples — they’re over the budget.")
Output:
Don’t add apples — they’re over the budget.
Why Use if-else?
The if
part checks your condition. If it’s true, that block runs.
If not, the else
part jumps in and executes instead.
This allows us to write programs that respond to real-world choices, like budget checks, age verifications, and more.