Mastering Conditional Statements in Python
PYTHON
Kishore Babu Valluri
4/24/20252 min read


When you're writing a program, you often need it to make decisions. Thatโs where conditional statements and control flow come in. Python makes this easy and readable.
In this blog post, weโll cover:
if, elif, and else statements
break and continue for controlling loops
Real-life examples
Letโs jump in!
1. Conditional Statements in Python
Conditional statements help your program decide what to do based on conditions.
Basic Structure:
if condition:
# code block
elif another_condition:
# another block
else:
# fallback block
How It Works
Python checks the if condition. If itโs True, it runs that block. If not, it checks elif. If nothing is True, it runs else.
Example 1: Age Checker
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
elif age > 0:
print("You are not eligible yet.")
else:
print("Invalid age entered.")
Explanation:
If age is 18 or more โ vote
Else if itโs a positive number โ too young
Otherwise โ invalid
Example 2: Grade System
score = float(input("Enter your exam score: "))
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: D")
Example 3: Odd or Even Number
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Example 4: Greeting Based on Time
hour = int(input("Enter the hour (0โ23): "))
if 0 <= hour < 12:
print("Good Morning!")
elif 12 <= hour < 17:
print("Good Afternoon!")
elif 17 <= hour < 21:
print("Good Evening!")
elif 21 <= hour < 24:
print("Good Night!")
else:
print("Invalid hour entered.")
Example 5: Shipping Cost Calculator
weight = float(input("Enter package weight (kg): "))
if weight <= 1:
print("Shipping cost: โน50")
elif weight <= 5:
print("Shipping cost: โน100")
elif weight <= 10:
print("Shipping cost: โน150")
else:
print("Shipping cost: โน200")
Example 6: BMI (Body Mass Index) Categorizer
weight = float(input("Enter weight in kg: "))
height = float(input("Enter height in meters: "))
bmi = weight / (height ** 2)
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal weight")
elif bmi < 30:
print("Overweight")
else:
print("Obese")
Example 7: Discount Based on Purchase Amount
amount = float(input("Enter purchase amount: "))
if amount >= 10000:
discount = 0.2
elif amount >= 5000:
discount = 0.1
elif amount >= 1000:
discount = 0.05
else:
discount = 0.0
final_price = amount * (1 - discount)
print(f"Discount Applied: โน{amount - final_price:.2f}")
print(f"Total to Pay: โน{final_price:.2f}")
Example 8: Leap Year Checker
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Example 7: Rock-Paper-Scissors (Basic Logic)
user = input("Enter rock, paper, or scissors: ").lower()
computer = "rock" # hardcoded for simplicity
if user == computer:
print("It's a tie!")
elif (user == "rock" and computer == "scissors") or \
(user == "paper" and computer == "rock") or \
(user == "scissors" and computer == "paper"):
print("You win!")
else:
print("You lose!")
Key Takeaways: Conditional Statements in Python
if, elif, and else help your program make decisions.
They control the flow based on whether a condition is True or False.Conditions are checked in order.
Python executes the first condition that is true and skips the rest.elif means "else if".
You can have multiple elif branches to handle different scenarios.else is optional.
It runs only if all previous conditions are false โ great for fallback logic.Use comparison and logical operators in conditions:
==, !=, >, <, >=, <=
and, or, not for combining conditions
Indentation matters.
Python uses indentation (typically 4 spaces) to define blocks under each condition.Real-life uses include:
Validating inputs
Decision-making in calculators
Grading systems
Form validations
Game logic


Kishore Babu Valluri
Senior Data Scientist | Freelance Consultant | AI/ML & GenAI Expert
With deep expertise in machine learning, artificial intelligence, and Generative AI, I work as a Senior Data Scientist, freelance consultant, and AI agent developer. I help businesses unlock value through intelligent automation, predictive modeling, and cutting-edge AI solutions.