About These Assignments
This page contains 30+ practice assignments covering all conditional statement topics in Python. Each problem is categorized by topic and difficulty:
š” Tip: Try solving each problem on your own first. Click the "Show Solution" button only after you've attempted the problem. This approach will help you learn more effectively.
1. Decision Making 4 Questions
if num > 0:
print(f"{num} is positive")
# Output: 10 is positive
This simple if statement checks if the number is greater than 0. If true, it prints the message. This is the foundation of all decision-making in Python.
if score >= 40:
print("Pass")
else:
print("Fail")
# Output: Pass
This demonstrates two-way decision making. If the score is 40 or above, the student passes. Otherwise, they fail. This is a common pattern in grading systems.
amount = 3000
if amount > balance:
print("Insufficient balance!")
elif amount <= 0:
print("Invalid amount!")
elif amount % 100 != 0:
print("Amount must be multiple of 100")
else:
balance -= amount
print(f"Withdrawal successful! Balance: ā¹{balance}")
# Output: Withdrawal successful! Balance: ā¹2000
This demonstrates multiple condition checks using if-elif-else. Each condition handles a different validation rule ā balance check, amount validity, and withdrawal limits.
if num % 5 == 0:
print(f"{num} is divisible by 5")
# Output: 25 is divisible by 5
The modulus operator % checks if the remainder when divided by 5 is 0. If true, the number is divisible by 5.
2. if Statement 5 Questions
if statement ā conditional execution based on a single condition.
if age >= 18:
print("Eligible to vote!")
# Output: Eligible to vote!
The if statement checks if the age is 18 or above. If true, it prints the eligibility message. If false, nothing happens. This is the simplest form of conditional execution.
if num % 2 == 0:
print(f"{num} is even")
# Output: 8 is even
The modulus operator % checks if the number is divisible by 2. If the remainder is 0, the number is even. This is a common use of the if statement with arithmetic operators.
if len(password) >= 8:
print("Password is valid!")
# Output: Password is valid!
The len() function returns the length of the string. The if statement checks if it's at least 8 characters. This is a common pattern in form validation.
if char in 'aeiouAEIOU':
print(f"{char} is a vowel")
# Output: a is a vowel
The in operator checks if the character exists in the string of vowels. This is a common pattern in text analysis.
if num % 3 == 0 and num % 5 == 0:
print(f"{num} is divisible by both 3 and 5")
# Output: 15 is divisible by both 3 and 5
This uses the logical operator and to check both conditions simultaneously. The number must be divisible by both 3 and 5.
3. if-else Statement 5 Questions
if-else statement ā two-way decision making.
if num % 2 == 0:
print(f"{num} is even")
else:
print(f"{num} is odd")
# Output: 7 is odd
The if-else statement provides two paths. If the number is divisible by 2, it's even. Otherwise, it's odd. This handles all possible cases.
if total > 500:
discount = total * 0.10
final = total - discount
print(f"Discount: ā¹{discount}, Final: ā¹{final}")
else:
print(f"Final: ā¹{total}")
# Output: Discount: ā¹60.0, Final: ā¹540.0
This demonstrates using if-else for business logic. If the total is over 500, a 10% discount is applied. Otherwise, the full amount is charged.
rating = 4.5
if rating >= 4:
bonus = salary * 0.10
print(f"Bonus: ā¹{bonus}")
else:
print("No bonus")
# Output: Bonus: ā¹5000.0
This shows how if-else is used in HR systems. Based on performance rating, the employee either receives a bonus or not.
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
# Output: Negative
This uses if-elif-else to handle three possible cases. The first true condition executes and the rest are skipped.
if char.isupper():
print("Uppercase")
elif char.islower():
print("Lowercase")
else:
print("Not a letter")
# Output: Uppercase
The isupper() and islower() methods check if a character is uppercase or lowercase. This is useful in text processing.
4. if-elif-else Ladder 5 Questions
if-elif-else ladder ā handling multiple conditions in sequence.
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
print(f"Score: {score}, Grade: {grade}")
# Output: Score: 85, Grade: B
The if-elif-else ladder checks conditions in order. The first true condition executes and the rest are skipped. This is the standard pattern for grading systems.
if day_num == 1:
day = "Monday"
elif day_num == 2:
day = "Tuesday"
elif day_num == 3:
day = "Wednesday"
elif day_num == 4:
day = "Thursday"
elif day_num == 5:
day = "Friday"
elif day_num == 6:
day = "Saturday"
elif day_num == 7:
day = "Sunday"
else:
day = "Invalid day"
print(f"Day: {day}")
# Output: Day: Wednesday
This demonstrates mapping numeric values to strings using if-elif-else. Each condition checks for a specific number and returns the corresponding day.
if income <= 250000:
tax = 0
elif income <= 500000:
tax = (income - 250000) * 0.05
elif income <= 1000000:
tax = 12500 + (income - 500000) * 0.10
else:
tax = 62500 + (income - 1000000) * 0.20
print(f"Income: ā¹{income}, Tax: ā¹{tax:.2f}")
# Output: Income: ā¹750000, Tax: ā¹37500.00
This is a real-world tax calculation using if-elif-else. Each income slab has a different tax rate, demonstrating the power of conditional logic in financial applications.
if light == "red":
print("Stop")
elif light == "yellow":
print("Slow down")
elif light == "green":
print("Go")
else:
print("Invalid light")
# Output: Stop
This demonstrates mapping color to action using if-elif-else. Each color has a specific action associated with it.
if weight <= 1:
cost = 50
elif weight <= 5:
cost = 100
elif weight <= 10:
cost = 150
else:
cost = 200
print(f"Shipping cost: ā¹{cost}")
# Output: Shipping cost: ā¹100
This calculates shipping cost based on weight tiers. Each weight range has a different shipping cost.
5. Nested if-else 5 Questions
if-else ā decisions within decisions for complex logic.
password = "12345"
if username == "admin":
if password == "12345":
print("Login successful!")
else:
print("Invalid password!")
else:
print("Invalid username!")
# Output: Login successful!
The outer if checks the username. Only if it's correct, the inner if checks the password. This prevents checking the password for non-existent users, making the system more secure.
attendance = 80
if attendance >= 75:
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
print(f"Grade: {grade}")
else:
print("Attendance insufficient!")
# Output: Grade: B
The outer if checks attendance. Only if attendance is sufficient, the inner if-elif-else calculates the grade. This is a practical example of nested conditions in academic systems.
assignments = 5
if attendance >= 75:
if assignments >= 4:
print("Eligible for grade")
else:
print("Incomplete assignments")
else:
print("Attendance insufficient")
# Output: Eligible for grade
This uses nested if to check both attendance and assignment completion. The inner condition only checks if the outer condition is true.
balance = 5000
amount = 2000
if pin == 1234:
if amount <= balance:
if amount <= 10000:
print(f"Withdrawal successful!")
else:
print("Daily limit exceeded")
else:
print("Insufficient balance")
else:
print("Invalid PIN")
# Output: Withdrawal successful!
This demonstrates multiple levels of validation. Each condition depends on the previous one being true.
if num >= 1:
if num <= 100:
print(f"{num} is between 1 and 100")
else:
print(f"{num} is greater than 100")
else:
print(f"{num} is less than 1")
# Output: 50 is between 1 and 100
This uses nested if to check if a number falls within a range. The outer condition checks if it's ℠1, and the inner checks if it's ⤠100.
6. Ternary Operator 4 Questions
result = "Even" if num % 2 == 0 else "Odd"
print(f"{num} is {result}")
# Output: 7 is Odd
The ternary operator evaluates the condition num % 2 == 0. If true, it returns "Even". If false, it returns "Odd". This is a compact alternative to the full if-else statement.
max_num = a if a > b else b
print(f"Maximum: {max_num}")
# Output: Maximum: 20
The ternary operator compares a > b. If true, a is assigned to max_num. Otherwise, b is assigned. This is a clean, readable way to find the maximum of two values.
min_num = a if a < b else b
print(f"Minimum: {min_num}")
# Output: Minimum: 10
The ternary operator compares a < b. If true, a is assigned to min_num. Otherwise, b is assigned.
result = "Positive" if num > 0 else "Negative"
print(f"{num} is {result}")
# Output: -3 is Negative
The ternary operator evaluates num > 0. If true, it returns "Positive". If false, it returns "Negative".
7. Mixed Practice 4 Questions
if units <= 100:
bill = units * 5
elif units <= 200:
bill = 100 * 5 + (units - 100) * 7
else:
bill = 100 * 5 + 100 * 7 + (units - 200) * 10
print(f"Units: {units}, Bill: ā¹{bill}")
# Output: Units: 250, Bill: ā¹1700
This problem combines if-elif-else with arithmetic operations. It's a common pattern in utility billing, tax calculation, and tiered pricing systems.
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
# Output: 2024 is a leap year
This combines logical operators (or, and) with conditional statements. A year is a leap year if it's divisible by 400, or divisible by 4 but not 100. This is a classic interview question.
price = 2000
budget = 2500
payment = "card"
if available:
if price <= budget:
if payment in ["card", "cash"]:
print("Booking confirmed!")
else:
print("Invalid payment method")
else:
print("Budget exceeded")
else:
print("Room not available")
# Output: Booking confirmed!
This combines all conditional concepts in a real-world hotel booking system. It checks availability, budget, and payment method.
op = "+"
if op == "+":
result = a + b
elif op == "-":
result = a - b
elif op == "*":
result = a * b
elif op == "/":
if b != 0:
result = a / b
else:
result = "Error: Division by zero"
else:
result = "Invalid operator"
print(f"Result: {result}")
# Output: Result: 15
This combines if-elif-else with nested conditions for a simple calculator. It handles multiple operations and error cases.
Try It Yourself!
Use the interactive editor below to test your solutions or write your own code.
CONDITIONAL STATEMENTS PRACTICE
========================================
1. GRADE CALCULATOR
Score: 85, Grade: B
2. LOGIN SYSTEM
Login successful!
3. TERNARY OPERATOR
7 is Odd
ā Write your solutions here!
š Related Tutorials
šÆ Decision Making
Learn the fundamentals of decision making
š if Statement
Master the if statement
š if-else Statement
Master the if-else statement