Below are 50 practical Python questions focusing on if, nested if-else, and multiple elif
Each question includes:
- A scenario or requirement
- A code snippet demonstrating one possible solution
Answer:
Use the modulo operator % to determine if the number is divisible by 2.
num = 10
if num % 2 == 0:
print("Even")
else:
print("Odd")Answer:
Use if-elif-else conditions to classify the number.
num = -5
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")Answer:
Use if-else to check membership in vowels.
ch = 'e'
if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
print("Vowel")
else:
print("Consonant")Answer:
A leap year is divisible by 400, or divisible by 4 but not by 100.
year = 2020
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not Leap Year")Answer:
Use multiple elif conditions for ranges.
age = 45
if age < 13:
print("Child")
elif age < 20:
print("Teen")
elif age < 60:
print("Adult")
else:
print("Senior")Answer:
Use if-else to check string length.
password = "pass1234"
if len(password) >= 8:
print("Strong password")
else:
print("Weak password")Answer:
First check positivity, then even/odd inside another if.
num = 9
if num > 0:
if num % 2 == 0:
print("Positive Even")
else:
print("Positive Odd")
else:
print("Not Positive")Answer:
Use if-elif-else to compare.
a = 10
b = 15
if a == b:
print("Equal")
elif a > b:
print("a is larger")
else:
print("b is larger")Answer:
Use str methods isupper() and islower().
ch = 'H'
if ch.isupper():
print("Uppercase")
elif ch.islower():
print("Lowercase")
else:
print("Neither uppercase nor lowercase")Answer:
Use multiple conditions with and.
num = 30
if num % 3 == 0 and num % 5 == 0:
print("Divisible by 3 and 5")
else:
print("Not divisible by both")Answer:
Use multiple elif conditions.
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")Answer:
Use str.isdigit().
ch = '5'
if ch.isdigit():
print("Digit")
else:
print("Not a digit")Answer:
Check positivity, then range.
num = 150
if num > 0:
if num > 100:
print("Positive and greater than 100")
else:
print("Positive but 100 or less")
else:
print("Not positive")Answer:
Use multiple elif for temperature ranges.
temp = 10
if temp < 0:
print("Freezing")
elif temp < 15:
print("Cold")
elif temp < 25:
print("Warm")
else:
print("Hot")Answer:
Convert to string and check last character.
num = 75
num_str = str(num)
if num_str.endswith('5'):
print("Ends with 5")
else:
print("Does not end with 5")Answer:
Check >=0, then zero or positive inside another if.
num = 0
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive")
else:
print("Negative")Answer:
Use if-elif-else with character comparisons.
ch = '?'
if ch == '.' or ch == ',' or ch == '!' or ch == '?':
print("Punctuation")
else:
print("Not punctuation")Answer:
Check first character for vowels.
name = "Ashish"
first_char = name[0].lower()
if first_char == 'a' or first_char == 'e' or first_char == 'i' or first_char == 'o' or first_char == 'u':
print("Starts with vowel")
else:
print("Does not start with vowel")- <6: Early Morning
- <12: Morning
- <18: Afternoon
- <24: Night
Answer:
Use multiple elif conditions.
hour = 14
if hour < 6:
print("Early Morning")
elif hour < 12:
print("Morning")
elif hour < 18:
print("Afternoon")
elif hour < 24:
print("Night")
else:
print("Invalid hour")Answer:
Use if-elif-else to check length ranges.
username = "Ashish"
length = len(username)
if length < 5:
print("Too short")
elif length > 10:
print("Too long")
else:
print("Valid length")Answer:
Check even first, then divisibility by 4.
num = 8
if num % 2 == 0:
if num % 4 == 0:
print("Even and divisible by 4")
else:
print("Even but not divisible by 4")
else:
print("Odd")22. Determine if a letter is uppercase vowel, lowercase vowel, uppercase consonant, or lowercase consonant.
Answer:
Use multiple if-elif conditions.
ch = 'U'
vowels = 'aeiouAEIOU'
if ch in vowels:
if ch.isupper():
print("Uppercase vowel")
else:
print("Lowercase vowel")
else:
if ch.isupper():
print("Uppercase consonant")
else:
print("Lowercase consonant")Answer:
Use if-else with range conditions.
num = 50
if 1 <= num <= 100:
print("Within range")
else:
print("Out of range")- Single-digit (|num| < 10)
- Two-digit (|num| < 100)
- Three or more digits (|num| >= 100)
Answer:
Use abs() and multiple elif.
num = 999
abs_num = abs(num)
if abs_num < 10:
print("Single-digit")
elif abs_num < 100:
print("Two-digit")
else:
print("Three or more digits")25. Nested if: If a number is positive, check if it's prime or not by a simple method (just check divisibility by 2 and 3).
Answer:
This is limited without loops or data structures, but let's just show conditions.
num = 9
if num > 0:
if num == 2 or num == 3:
print("Prime")
elif num % 2 != 0 and num % 3 != 0:
print("Likely prime")
else:
print("Not prime")
else:
print("Not positive")Answer:
Use str.isupper() and str.islower().
text = "HELLO"
if text.isupper():
print("All uppercase")
elif text.islower():
print("All lowercase")
else:
print("Mixed case")- <18.5: Underweight
- <25: Normal
- <30: Overweight
- else: Obese
Answer:
Use multiple elif for ranges.
bmi = 22
if bmi < 18.5:
print("Underweight")
elif bmi < 25:
print("Normal")
elif bmi < 30:
print("Overweight")
else:
print("Obese")Answer:
Check if char == ' '.
ch = ' '
if ch == ' ':
print("Whitespace")
else:
print("Not whitespace")Answer:
First check non-negative, then divisibility by 10.
num = 40
if num >= 0:
if num % 10 == 0:
print("Non-negative multiple of 10")
else:
print("Non-negative but not multiple of 10")
else:
print("Negative")- <=1kg: $5
- <=5kg: $10
- <=20kg: $20
- else: $50
Answer:
Use multiple elif.
weight = 6
if weight <= 1:
print("$5")
elif weight <= 5:
print("$10")
elif weight <= 20:
print("$20")
else:
print("$50")Answer:
Check first char using isupper().
text = "Hello"
if text and text[0].isupper():
print("Starts with capital letter")
else:
print("Does not start with capital letter")32. Nested if: If a number is positive, check if it is a perfect square of an integer (just check num==4 or num==9 for simplicity).
Answer:
Restricted check without loops.
num = 9
if num > 0:
if num == 4 or num == 9:
print("Positive perfect square (from limited check)")
else:
print("Positive but not perfect square (from limited check)")
else:
print("Not positive")- <90: Acute
- ==90: Right
- <180: Obtuse
- ==180: Straight
- else: Reflex
Answer:
Use multiple if-elif.
angle = 120
if angle < 90:
print("Acute")
elif angle == 90:
print("Right")
elif angle < 180:
print("Obtuse")
elif angle == 180:
print("Straight")
else:
print("Reflex")Answer:
Use abs difference.
num = 95
if abs(num - 100) <= 10:
print("Close to 100")
else:
print("Not close")35. Nested if: Check if a given code is 'admin', if yes check if 'active' is True, else print restricted access.
Answer:
Simulate user_role and active condition.
user_role = 'admin'
active = True
if user_role == 'admin':
if active:
print("Full Access")
else:
print("Admin not active")
else:
print("Restricted Access")- <30: Slow
- <60: Normal
- <100: Fast
- else: Very Fast
Answer:
Use multiple elif.
speed = 75
if speed < 30:
print("Slow")
elif speed < 60:
print("Normal")
elif speed < 100:
print("Fast")
else:
print("Very Fast")37. Determine if a given character is uppercase vowel, uppercase consonant, or not an uppercase letter at all.
Answer:
Nested logic on uppercase and vowels.
ch = 'E'
if ch.isupper():
if ch == 'A' or ch == 'E' or ch == 'I' or ch == 'O' or ch == 'U':
print("Uppercase vowel")
else:
print("Uppercase consonant")
else:
print("Not uppercase letter")Answer:
Simple if-elif-else.
num = 0
if num < 0:
print("Negative")
elif num == 0:
print("Zero")
else:
print("Positive")Answer:
No data structures, just basic conditions.
text = "madam"
if text and text[0] == text[-1]:
print("Potential palindrome (first and last char match)")
else:
print("Not palindrome based on first and last char")-
100000: "High earner"
-
50000: "Mid earner"
-
20000: "Low earner"
- else: "Very low"
Answer:
Use multiple elif.
salary = 30000
if salary > 100000:
print("High earner")
elif salary > 50000:
print("Mid earner")
elif salary > 20000:
print("Low earner")
else:
print("Very low")Answer:
Check if letter using isalpha(), then case.
ch = 'G'
if ch.isalpha():
if ch.isupper():
print("Uppercase letter")
else:
print("Lowercase letter")
else:
print("Not a letter")Answer:
Use multiple if/elif.
num = 9
if num % 2 == 0:
print("Divisible by 2")
elif num % 3 == 0:
print("Divisible by 3")
elif num % 5 == 0:
print("Divisible by 5")
else:
print("Not divisible by 2, 3, or 5")Answer:
Use len() % 2.
text = "Hello"
if len(text) % 2 == 0:
print("Even length")
else:
print("Odd length")Answer:
Two-level check.
num = 60
if num > 10:
if num > 50:
print("Greater than 50")
else:
print("Between 10 and 50")
else:
print("10 or less")- 1-5: Weekday
- 6: Saturday
- 7: Sunday
- else: Invalid
Answer:
Use if-elif-else.
day = 7
if day >= 1 and day <= 5:
print("Weekday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("Invalid")Answer:
Use is None.
var = None
if var is None:
print("var is None")
else:
print("var has a value")Answer:
Check digit first, then numeric value.
ch = '4'
if ch.isdigit():
digit_value = int(ch)
if digit_value % 2 == 0:
print("Even digit")
else:
print("Odd digit")
else:
print("Not a digit")- <1 deg/s: Very slow
- <10 deg/s: Slow
- <100 deg/s: Moderate
- else: Fast
Answer:
Use multiple elif.
speed = 5
if speed < 1:
print("Very slow")
elif speed < 10:
print("Slow")
elif speed < 100:
print("Moderate")
else:
print("Fast")Answer:
Use conditions on first and last char.
text = "ABCZ"
if text and text[0] == 'A' and text[-1] == 'Z':
print("Starts with A and ends with Z")
else:
print("Condition not met")50. Nested if: If year is divisible by 4, check if also divisible by 100. If yes, then must be divisible by 400 for leap year.
Answer:
Detailed leap year check.
year = 2000
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print("Leap Year")
else:
print("Not Leap Year")
else:
print("Leap Year")
else:
print("Not Leap Year")If you found this helpful, please consider following or starring the repository!
Follow me on:
Stay updated with my latest content and projects!