Practice Questions — Conditional Statements in Python (if, elif, else)
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output?
x = 10
if x > 5:
print("Big")
print("Done")Check if 10 > 5. The last line is outside the if block.
BigDoneQuestion 2
Easy
What is the output?
x = 3
if x > 5:
print("Big")
print("Done")Is 3 > 5?
DoneQuestion 3
Easy
What is the output?
num = 7
if num % 2 == 0:
print("Even")
else:
print("Odd")What is 7 % 2?
OddQuestion 4
Easy
What is the output?
marks = 85
if marks >= 90:
print("A")
elif marks >= 80:
print("B")
elif marks >= 70:
print("C")
else:
print("D")Check conditions from top to bottom. Which is the first True condition?
BQuestion 5
Easy
What is the output?
a = 5
b = 5
if a == b:
print("Equal")
else:
print("Not equal")Does 5 equal 5?
EqualQuestion 6
Medium
What is the output?
x = 15
if x > 10:
print("A")
if x > 5:
print("B")
if x > 0:
print("C")These are three separate if statements, NOT if-elif-else.
ABCQuestion 7
Medium
What is the output? Compare with the previous question.
x = 15
if x > 10:
print("A")
elif x > 5:
print("B")
elif x > 0:
print("C")This time it IS an if-elif chain. Only the first True condition's block runs.
AQuestion 8
Medium
What is the output?
x = 10
y = 20
if x > 5 and y > 15:
print("Both")
elif x > 5 or y > 15:
print("At least one")
else:
print("Neither")Check if BOTH conditions are True for the 'and'.
BothQuestion 9
Medium
What is the output?
if 0:
print("A")
if "":
print("B")
if None:
print("C")
if "hello":
print("D")
if 1:
print("E")0, empty string, and None are falsy. Non-empty string and non-zero integer are truthy.
DEQuestion 10
Medium
What is the output?
result = "Pass" if 85 >= 40 else "Fail"
print(result)This is a ternary operator. Check the condition.
PassQuestion 11
Hard
What is the output?
x = 5
if x > 3:
if x > 7:
print("A")
else:
print("B")
else:
if x > 1:
print("C")
else:
print("D")First check the outer if. Then check the inner if within the matching block.
BQuestion 12
Hard
What is the output?
a = 10
b = 20
c = 30
if a > b:
print("X")
elif b > c:
print("Y")
elif a + b > c:
print("Z")
else:
print("W")Check each condition: 10 > 20? 20 > 30? 10 + 20 > 30?
WQuestion 13
Hard
What is the output?
if "False":
print("A")
if bool(""):
print("B")
if bool(0):
print("C")
if bool("0"):
print("D")The string 'False' is a non-empty string. The string '0' is also non-empty.
ADQuestion 14
Hard
What is the output?
x = 5
result = "A" if x > 10 else "B" if x > 3 else "C"
print(result)Nested ternary: evaluate from left to right.
BQuestion 15
Hard
What is the output?
x = 10
y = 5
z = 10
if x == z:
if y > x:
print("A")
elif y == x:
print("B")
else:
print("C")
elif x > y:
print("D")
else:
print("E")x equals z (both 10). Then check y vs x inside.
CQuestion 16
Hard
What is the output?
print("Yes") if True else print("No")
print("Yes") if False else print("No")The ternary operator can contain function calls, not just values.
YesNoQuestion 17
Medium
What is the difference between using separate if statements and using if-elif? Give an example where the output differs.
With separate if statements, every condition is checked. With if-elif, checking stops at the first True.
With separate if statements, every condition is checked independently. With if-elif, checking stops at the first True condition. Example: for
x = 15, three separate if x > 10, if x > 5, if x > 0 would print all three blocks. But if x > 10, elif x > 5, elif x > 0 would print only the first one.Question 18
Medium
List all the falsy values in Python.
There are 7 main categories of falsy values.
The falsy values in Python are:
False, 0 (integer zero), 0.0 (float zero), "" (empty string), [] (empty list), () (empty tuple), {} (empty dictionary), and None.Question 19
Hard
What is the output?
x = 0
if x:
print("A")
if not x:
print("B")
if x == 0:
print("C")
if x is None:
print("D")0 is falsy, but it is NOT None. Also, 0 == 0 is True.
BCQuestion 20
Hard
What is the output?
a = True
b = False
c = True
if a and b or c:
print("X")
if (a and b) or c:
print("Y")
if a and (b or c):
print("Z")Remember: 'and' has higher precedence than 'or'.
XYZMixed & Application Questions
Question 1
Easy
Write a program that takes a number from the user and prints whether it is positive, negative, or zero.
Use if-elif-else with comparisons to 0.
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")Question 2
Easy
What is the output?
age = 16
if age >= 18:
print("Can vote")
print("Done")16 is not >= 18. The last line is outside the if block.
DoneQuestion 3
Medium
What is the output?
x = 5
if x > 2:
print("A")
if x > 4:
print("B")
print("C")
print("D")Trace the indentation carefully. 'C' is inside the outer if but outside the inner if.
ABCDQuestion 4
Medium
Ananya wants to build a simple eligibility checker. Write a program that takes age and nationality as input. Print 'Eligible' if age >= 18 AND nationality is 'Indian'. Otherwise, print 'Not Eligible'.
Use 'and' to combine two conditions.
age = int(input("Enter age: "))
nationality = input("Enter nationality: ")
if age >= 18 and nationality == "Indian":
print("Eligible")
else:
print("Not Eligible")Question 5
Medium
What is the output?
num = 15
if num % 3 == 0 and num % 5 == 0:
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
else:
print(num)Check: is 15 divisible by both 3 and 5?
FizzBuzzQuestion 6
Medium
What is the output?
x = []
if x:
print("List has items")
else:
print("List is empty")
y = [0, 0, 0]
if y:
print("List has items")
else:
print("List is empty")An empty list is falsy. A list with items (even zeros) is truthy.
List is emptyList has itemsQuestion 7
Hard
What is the output?
a = 5
b = 10
c = 15
if a + b == c:
print("Sum")
if a * b == c * a:
print("Product")
if c - b == a:
print("Difference")
if c / a == b / a:
print("Quotient")These are four separate if statements. Check each one independently.
SumDifferenceQuestion 8
Hard
What is the output?
x = 10
if x > 5:
x = x - 3
if x > 5:
x = x - 3
if x > 5:
print("Still big")
else:
print("Getting small")
else:
print("Small")
print("x =", x)Trace x: starts at 10, becomes 7 after first subtraction, then becomes 4.
Getting smallx = 4Question 9
Hard
What is the output?
name = ""
if name:
greeting = "Hello, " + name
else:
greeting = "Hello, stranger"
short = "Hello, " + name if name else "Hello, stranger"
print(greeting)
print(short)Empty string is falsy. Both approaches should give the same result.
Hello, strangerHello, strangerQuestion 10
Medium
Write a program that takes three numbers from the user and prints the largest one.
You can compare them with if-elif-else, or use nested conditions.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)Question 11
Hard
What is the output?
x = 1
y = 2
z = 3
if x < y < z:
print("A")
if x < y > z:
print("B")
if x < y and y < z:
print("C")
if x == 1 == True:
print("D")Chained comparisons. Also, 1 == True in Python.
ACDQuestion 12
Hard
What is the output?
x = None
if x is None:
print("A")
if x == None:
print("B")
if not x:
print("C")
if x == False:
print("D")None is falsy. None is None. But is None == False?
ABCMultiple Choice Questions
MCQ 1
What keyword starts a conditional statement in Python?
Answer: C
C is correct. The
C is correct. The
if keyword is used to start a conditional statement in Python. The other options are not Python keywords for conditional statements.MCQ 2
What must appear at the end of an if statement line?
Answer: B
B is correct. Every
B is correct. Every
if, elif, and else line must end with a colon (:). Semicolons are used to separate statements on one line (rare). Curly braces are used in languages like Java and C++, not Python.MCQ 3
What does elif stand for?
Answer: A
A is correct.
A is correct.
elif is short for "else if". It allows you to check additional conditions when the previous if or elif was False.MCQ 4
How many spaces is the standard indentation in Python?
Answer: C
C is correct. The standard Python indentation is 4 spaces. While Python technically allows any consistent number of spaces, PEP 8 (Python's style guide) recommends 4 spaces.
C is correct. The standard Python indentation is 4 spaces. While Python technically allows any consistent number of spaces, PEP 8 (Python's style guide) recommends 4 spaces.
MCQ 5
Which value is considered falsy in Python?
Answer: D
D is correct.
D is correct.
0 is falsy. All the other options are truthy: 1 is a non-zero number, "hello" is a non-empty string, and [1, 2, 3] is a non-empty list.MCQ 6
What is the output of: print('Adult' if 15 >= 18 else 'Minor')?
Answer: B
B is correct. The ternary operator checks
B is correct. The ternary operator checks
15 >= 18 which is False. So the value after else is chosen: 'Minor'.MCQ 7
What happens if you forget to indent the code after an if statement?
Answer: C
C is correct. Python requires indentation after
C is correct. Python requires indentation after
if, elif, else, and other compound statements. Missing indentation causes an IndentationError. Python never adds indentation automatically.MCQ 8
In an if-elif-else chain, how many blocks can execute?
Answer: D
D is correct when an else block is present. In an if-elif-else chain, exactly one block executes: either the first if/elif whose condition is True, or the else block if no conditions are True. Without an else, at most one block executes (option B would be correct).
D is correct when an else block is present. In an if-elif-else chain, exactly one block executes: either the first if/elif whose condition is True, or the else block if no conditions are True. Without an else, at most one block executes (option B would be correct).
MCQ 9
What is the output of: print(bool('False'))?
Answer: A
A is correct.
A is correct.
'False' is a non-empty string, which is truthy. bool('False') returns True. Python does not interpret the content of the string; it only checks whether the string is empty.MCQ 10
Which is the correct ternary operator syntax in Python?
Answer: B
B is correct. Python's ternary syntax is
B is correct. Python's ternary syntax is
value_if_true if condition else value_if_false. Option A is the syntax used in C, Java, and JavaScript. Options C and D are not valid Python syntax.MCQ 11
What is the output?
x = 10
if x > 5: print('A')
else: print('B')Answer: A
A is correct. Short-hand if-else on one line is valid when each block has only one statement.
A is correct. Short-hand if-else on one line is valid when each block has only one statement.
10 > 5 is True, so 'A' prints.MCQ 12
What is the output?
print(None == False)Answer: B
B is correct.
B is correct.
None == False is False. Although both are falsy (they are treated as False in boolean context), they are not equal to each other. None is only equal to None. Similarly, 0 == False is True (because bool is a subclass of int), but None == False is False.MCQ 13
What error occurs with this code?
if True:
print('A')
print('B')Answer: B
B is correct. The second print has 2 spaces of indentation while the first has 4 spaces. All lines in the same block must have the same indentation level. Python raises an
B is correct. The second print has 2 spaces of indentation while the first has 4 spaces. All lines in the same block must have the same indentation level. Python raises an
IndentationError: unexpected indent.MCQ 14
What is the output?
x = 5
y = 10
print('Equal' if x == y else 'X bigger' if x > y else 'Y bigger')Answer: C
C is correct. Nested ternary:
C is correct. Nested ternary:
5 == 10 is False, go to else. 5 > 10 is False, go to else. Result: 'Y bigger'.MCQ 15
Which of these is truthy in Python?
Answer: D
D is correct.
D is correct.
'0' is a non-empty string, making it truthy. 0 (integer zero), '' (empty string), and None are all falsy. This is a frequently tested concept.MCQ 16
What is 1 < 2 < 3 < 4 equivalent to in Python?
Answer: B
B is correct. Python's chained comparisons are equivalent to each adjacent pair connected by
B is correct. Python's chained comparisons are equivalent to each adjacent pair connected by
and. 1 < 2 < 3 < 4 means 1 < 2 and 2 < 3 and 3 < 4. Option A is how most other languages would evaluate it (left to right, which would give a different result). Option D is what option A would produce.MCQ 17
Which keyword catches all remaining cases in a conditional chain?
Answer: B
B is correct. The
B is correct. The
else keyword catches all cases not handled by the preceding if and elif blocks. default is used in switch statements in other languages (Python 3.10+ uses case _: for match-case). otherwise is not a Python keyword.MCQ 18
What is the output?
if not not True:
print('A')
else:
print('B')Answer: A
A is correct.
A is correct.
not True = False. not False = True. So not not True = True, and 'A' prints. Double negation returns the original value.MCQ 19
Can an if-elif chain exist without an else block?
Answer: B
B is correct. The
B is correct. The
else block is optional. An if-elif chain without else simply means: if no condition is True, nothing happens (no block executes).MCQ 20
What is the output?
x = 10
if x > 5:
pass
else:
print('Small')
print('Done')Answer: B
B is correct.
B is correct.
10 > 5 is True, so the if block runs. But the if block contains only pass, which does nothing. The else block is skipped. "Done" is outside the if-else, so it prints. Output: just "Done".Coding Challenges
Challenge 1: Voting Eligibility
EasyWrite a program that takes a user's age and prints whether they are eligible to vote (age >= 18). Print 'You can vote!' or 'You cannot vote yet. Wait [X] more years.' where X is the years remaining.
Sample Input
Enter your age: 15
Sample Output
You cannot vote yet. Wait 3 more years.
Use if-else. Calculate remaining years for the else case.
age = int(input("Enter your age: "))
if age >= 18:
print("You can vote!")
else:
years_left = 18 - age
print("You cannot vote yet. Wait", years_left, "more years.")Challenge 2: Ticket Price Calculator
EasyA movie theater charges different prices: Children (under 12): Rs 100, Teens (12-17): Rs 200, Adults (18-59): Rs 300, Seniors (60+): Rs 150. Take age as input and print the ticket price.
Sample Input
Enter age: 14
Sample Output
Ticket price: Rs 200
Use if-elif-else chain.
age = int(input("Enter age: "))
if age < 12:
price = 100
elif age <= 17:
price = 200
elif age <= 59:
price = 300
else:
price = 150
print("Ticket price: Rs", price)Challenge 3: Triangle Type Identifier
MediumTake three sides of a triangle from the user. First check if they can form a valid triangle (sum of any two sides must be greater than the third). If valid, determine if it is equilateral (all equal), isosceles (two equal), or scalene (none equal).
Sample Input
Side 1: 5
Side 2: 5
Side 3: 8
Sample Output
Valid triangle
Type: Isosceles
Check validity first. Then check type. Use logical operators.
a = int(input("Side 1: "))
b = int(input("Side 2: "))
c = int(input("Side 3: "))
if a + b > c and b + c > a and a + c > b:
print("Valid triangle")
if a == b == c:
print("Type: Equilateral")
elif a == b or b == c or a == c:
print("Type: Isosceles")
else:
print("Type: Scalene")
else:
print("Not a valid triangle")Challenge 4: Grade Calculator with Remarks
MediumTake marks (0-100) from the user. Validate that marks are between 0 and 100. Then assign grades: A (90-100), B (80-89), C (70-79), D (60-69), E (40-59), F (below 40). Also print a remark: A/B = 'Excellent', C/D = 'Good', E = 'Pass', F = 'Fail'.
Sample Input
Enter marks: 73
Sample Output
Grade: C
Remark: Good
Validate input range first. Use if-elif-else.
marks = int(input("Enter marks: "))
if marks < 0 or marks > 100:
print("Invalid marks! Must be 0-100.")
elif marks >= 90:
print("Grade: A")
print("Remark: Excellent")
elif marks >= 80:
print("Grade: B")
print("Remark: Excellent")
elif marks >= 70:
print("Grade: C")
print("Remark: Good")
elif marks >= 60:
print("Grade: D")
print("Remark: Good")
elif marks >= 40:
print("Grade: E")
print("Remark: Pass")
else:
print("Grade: F")
print("Remark: Fail")Challenge 5: Leap Year Checker
MediumTake a year from the user and determine if it is a leap year. Rules: A year is a leap year if (1) it is divisible by 4, AND (2) it is NOT divisible by 100, UNLESS (3) it is also divisible by 400. Test with: 2024, 1900, 2000.
Sample Input
Enter year: 1900
Sample Output
1900 is NOT a leap year
Use logical operators (and, or, not). Handle all three rules.
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is NOT a leap year")Challenge 6: Simple Calculator with Operation Choice
MediumBuild a calculator that takes two numbers and an operation (+, -, *, /) from the user. Perform the chosen operation and print the result. Handle division by zero.
Sample Input
Enter first number: 20
Enter second number: 0
Enter operation (+, -, *, /): /
Sample Output
Error: Cannot divide by zero!
Use if-elif-else for operation selection. Check for division by zero.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")
if op == "+":
print("Result:", num1 + num2)
elif op == "-":
print("Result:", num1 - num2)
elif op == "*":
print("Result:", num1 * num2)
elif op == "/":
if num2 == 0:
print("Error: Cannot divide by zero!")
else:
print("Result:", num1 / num2)
else:
print("Invalid operation!")Challenge 7: BMI Calculator with Health Category
HardTake weight (in kg) and height (in meters) from the user. Calculate BMI using the formula: BMI = weight / (height * height). Classify: Underweight (BMI < 18.5), Normal (18.5-24.9), Overweight (25-29.9), Obese (30+). Print BMI rounded to 1 decimal place and the category.
Sample Input
Enter weight (kg): 70
Enter height (m): 1.75
Sample Output
BMI: 22.9
Category: Normal weight
Use float(input()) for both values. Validate that both are positive. Round BMI to 1 decimal.
weight = float(input("Enter weight (kg): "))
height = float(input("Enter height (m): "))
if weight <= 0 or height <= 0:
print("Error: Weight and height must be positive.")
else:
bmi = weight / (height ** 2)
bmi_rounded = round(bmi, 1)
print("BMI:", bmi_rounded)
if bmi < 18.5:
print("Category: Underweight")
elif bmi < 25:
print("Category: Normal weight")
elif bmi < 30:
print("Category: Overweight")
else:
print("Category: Obese")Challenge 8: Electricity Bill Calculator
HardWrite a program for Rohan to calculate his electricity bill. Take units consumed as input. Pricing: First 100 units: Rs 5/unit. Next 100 units (101-200): Rs 7/unit. Next 100 units (201-300): Rs 10/unit. Above 300: Rs 15/unit. Add a fixed charge of Rs 100. Print the total bill.
Sample Input
Enter units consumed: 250
Sample Output
Units: 250
Total bill: Rs 1600
Use if-elif-else. Calculate charges for each slab separately.
units = int(input("Enter units consumed: "))
fixed_charge = 100
if units <= 100:
bill = units * 5
elif units <= 200:
bill = 100 * 5 + (units - 100) * 7
elif units <= 300:
bill = 100 * 5 + 100 * 7 + (units - 200) * 10
else:
bill = 100 * 5 + 100 * 7 + 100 * 10 + (units - 300) * 15
total = bill + fixed_charge
print("Units:", units)
print("Total bill: Rs", total)Challenge 9: Number Classifier
HardTake a number from the user and print ALL properties that apply: positive/negative/zero, even/odd, divisible by 5, divisible by 3, a single digit number, a two-digit number, a three-digit number. Use separate if statements (not elif) since multiple properties can apply.
Sample Input
Enter a number: 15
Sample Output
15 is positive
15 is odd
15 is divisible by 5
15 is divisible by 3
15 is a two-digit number
Use separate if statements (not elif) since multiple conditions can be true simultaneously.
num = int(input("Enter a number: "))
if num > 0:
print(num, "is positive")
elif num < 0:
print(num, "is negative")
else:
print(num, "is zero")
if num != 0:
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")
if num % 5 == 0 and num != 0:
print(num, "is divisible by 5")
if num % 3 == 0 and num != 0:
print(num, "is divisible by 3")
abs_num = abs(num)
if 0 <= abs_num <= 9:
print(num, "is a single digit number")
elif 10 <= abs_num <= 99:
print(num, "is a two-digit number")
elif 100 <= abs_num <= 999:
print(num, "is a three-digit number")Challenge 10: Password Strength Checker
HardTake a password as input and check its strength. Rules: Weak (length < 6), Medium (length 6-9 and contains at least one digit), Strong (length 10+ and contains at least one digit and at least one uppercase letter). Print the strength level. Hint: Use any() with a generator expression or loop through characters.
Sample Input
Enter password: Hello123World
Sample Output
Password strength: Strong
Use len() for length. Use the 'in' operator or string methods to check for digits and uppercase. Use nested conditions.
password = input("Enter password: ")
length = len(password)
has_digit = False
has_upper = False
for char in password:
if char.isdigit():
has_digit = True
if char.isupper():
has_upper = True
if length >= 10 and has_digit and has_upper:
print("Password strength: Strong")
elif length >= 6 and has_digit:
print("Password strength: Medium")
else:
print("Password strength: Weak")Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn Python with a live mentor?
Explore our Python course