Python Conditional Basics
Every program needs to make decisions — whether it’s checking if a user is old enough to vote, verifying login details, or deciding if a stock should be bought or sold.
In Python, we make such decisions using conditional statements.
In this blog, you’ll learn:
- What conditional statements are
- The
if
,elif
, andelse
keywords - Comparison and logical operators
- Nested and shorthand conditionals
- Real-world examples
🧠 1. What Are Conditional Statements?
Conditional statements allow your program to execute certain blocks of code only if specific conditions are met.
For example:
“If it’s raining, take an umbrella. Else, go outside.”
In Python, we use:
if
– to check a conditionelif
– to check another condition if the previous one failselse
– to handle all other cases
🧩 2. The if
Statement
The simplest conditional in Python.
age = 18
if age >= 18:
print("You are eligible to vote.")
Output:
You are eligible to vote.
Here’s what happens:
- Python checks if
age >= 18
. - If True, it executes the
print()
statement. - If False, it skips that block.
Note: Python uses indentation (spaces) to define code blocks — not braces {}
like other languages.
🔁 3. The if...else
Statement
Sometimes you want one thing to happen if the condition is true, and something else if it’s false.
num = 10
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Output:
Even number
Here, the program checks if num
is divisible by 2.
If not, it executes the else
block.
🔄 4. The if...elif...else
Chain
Use elif
(short for else if) when you need to check multiple conditions.
marks = 75
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: D")
Output:
Grade: B
Here:
- Python checks each condition in order.
- Once a condition is
True
, it runs that block and skips the rest.
⚙️ 5. Comparison Operators in Python
Comparison operators are used to compare values in conditions.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 10 > 5 | True |
< | Less than | 3 < 8 | True |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 4 <= 2 | False |
🧩 6. Logical Operators
You can combine multiple conditions using logical operators:
Operator | Meaning | Example | Result |
---|---|---|---|
and | True if both conditions are true | x > 0 and x < 10 | True if x between 0 and 10 |
or | True if at least one condition is true | x > 10 or x == 5 | True if any condition is true |
not | Reverses the condition | not(x > 10) | True if x <= 10 |
Example:
age = 25
country = "India"
if age > 18 and country == "India":
print("Eligible for Indian voter ID.")
Output:
Eligible for Indian voter ID.
🔁 7. Nested if
Statements
You can put one if
inside another — this is called a nested condition.
age = 20
citizen = "Indian"
if age >= 18:
if citizen == "Indian":
print("Eligible to vote in India.")
else:
print("Not an Indian citizen.")
else:
print("Not old enough to vote.")
Output:
Eligible to vote in India.
⚡ 8. Shorthand if
Statement (One-Liner)
Python allows compact single-line conditionals.
x = 10
print("Positive") if x > 0 else print("Non-positive")
Output:
Positive
This is useful for quick, simple checks.
🎯 9. Real-World Example — Checking Login Access
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Access granted.")
else:
print("Access denied.")
Output:
Access granted.
🧮 10. Example — Number Classification
Let’s build a program that checks whether a number is positive, negative, or zero.
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num < 0:
print("Negative number")
else:
print("Zero")
Output (if user enters -5):
Negative number
🧠 11. Example — Leap Year Checker
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Output:
Leap year
🔍 12. Common Mistakes to Avoid
❌ Missing indentation:
if True:
print("Hello") # ❌ Wrong
✅ Correct:
if True:
print("Hello")
🧾 Summary
Concept | Description | Example |
---|---|---|
if | Executes if condition is true | if x > 0: |
if...else | Executes one of two blocks | if x > 0: ... else: ... |
elif | Checks multiple conditions | if...elif...else |
and, or, not | Combine multiple conditions | if x > 0 and y < 5: |
Nested if | One if inside another | if x > 0: if y > 0: |
Shorthand if | Single-line conditional | "Yes" if True else "No" |
💡 Key Takeaways
✅ Conditional statements help programs make decisions
✅ Python uses indentation to define code blocks
✅ Use and
, or
, not
for combining conditions
✅ Always test your logic with different inputs
🧱 Example Challenge for You
Try writing a Python program that:
- Takes a user’s marks as input
- Prints whether the user passed, failed, or got distinction