Estimated reading time: 3.0 mins read
Conditional statements are one of the most important building blocks in Python programming. They allow your program to make decisions based on conditions, just like we do in real life.
For example:
- If it’s raining → take an umbrella
- If marks ≥ 40 → pass
- If age ≥ 18 → eligible to vote
In Python, we use conditional statements to implement this logic.
What Are Conditional Statements?
Conditional statements help the program execute different blocks of code depending on whether a condition is True or False.
Python supports the following conditional statements:
ifif elseif elif else- Nested
if - Shorthand
if (ternary operator)
if Statement in Python
The if statement executes a block of code only if the condition is True.
Syntax
if condition:
# code executes if condition is True
Example
age = 20
if age >= 18:
print("You are eligible to vote")
Output
You are eligible to vote
👉 If the condition is False, nothing happens.
if else Statement
The if else statement executes one block if the condition is True and another block if it is False.
Syntax
if condition:
# True block
else:
# False block
Example
marks = 35
if marks >= 40:
print("You passed the exam")
else:
print("You failed the exam")
Output
You failed the exam
if elif else Statement
When you have multiple conditions, use elif (else if).
Syntax
if condition1:
# block 1
elif condition2:
# block 2
else:
# default block
Example
score = 85
if score >= 90:
print("Grade A")
elif score >= 75:
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Fail")
Output
Grade B
👉 Python checks conditions from top to bottom and stops when it finds a True condition.
Nested if Statements
An if statement inside another if is called nested if.
Example
age = 22
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
else:
print("ID required")
else:
print("Underage")
Output
Entry allowed
👉 Use nested if carefully to keep code readable.
Shorthand if (Ternary Operator)
Python allows writing simple if else in one line.
Syntax
result_if_true if condition else result_if_false
Example
a = 10
b = 20
print("A is greater") if a > b else print("B is greater")
Output
B is greater
Comparison Operators Used in Conditions
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example
x = 5
print(x > 3) # True
print(x == 10) # False
Logical Operators in Conditional Statements
| Operator | Description |
|---|---|
and | True if both conditions are True |
or | True if any one condition is True |
not | Reverses the condition |
Example
age = 25
citizen = True
if age >= 18 and citizen:
print("Eligible to vote")
Common Mistakes Beginners Make
- Forgetting colon
:after condition - Using
=instead of== - Incorrect indentation
- Writing unnecessary nested
if
Real-Life Use Cases of Conditional Statements
- Login authentication
- Checking eligibility (age, marks, experience)
- Validating user input
- Decision-making in games and apps
- Form validations
Important Interview Qs&As
Q1. What are conditional statements in Python?
A1. Conditional statements allow a program to execute different code blocks based on True or False conditions.
Q2. What is the difference between if and elif?
if and elif?A2. if checks the first condition, while elif checks additional conditions if previous ones are False.
Q3. Can we write if without else?
if without else?A3. Yes, else is optional.
Q4. What is a nested if?
if?A4. An if statement inside another if block.
Q5. What is the ternary operator?
A5. A one-line shorthand way to write if else in Python.
Happy Learning !


Leave a Reply