If–Else Conditional Statements in Python
Conditional statements in Python are used to make decisions. The program executes different blocks of code depending on whether a condition is True or False.
💡 Decision-making is one of the most important concepts in programming.
What is an if Statement?
The if statement executes a block of code only when the given condition is true.
age = 18
if age >= 18:
print("You are eligible to vote")
if–else Statement
The else block runs when the if condition
is false.
number = 5
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
if–elif–else Statement
Use elif (else if) to check multiple conditions. Python checks conditions from top to bottom.
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Important Rules
📌 Follow these rules to avoid errors in conditional statements.
- Conditions must end with a colon
: - Indentation is mandatory in Python
elifcan be used multiple timeselseis optional
Real-Life Example
Checking whether a student has passed an exam:
marks = 32
if marks >= 35:
print("Pass")
else:
print("Fail")
❌ Incorrect indentation will cause an error in Python.
Practice
- Check if a number is positive or negative
- Write a program to check even or odd
- Check grade based on marks