Python Java SQL Course Notes C C++ HTML CSS JS

Python Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Conditional Statements Practice

if, elif, else — decision-making in Python programs.

1
Positive or Negative
Easy
Given an integer N, determine whether it is positive, negative, or zero.
A single integer N.
Print "Positive" if N > 0, "Negative" if N < 0, or "Zero" if N is 0.
Sample Input 1
-5
Sample Output 1
Negative
Sample Input 2
0
Sample Output 2
Zero
Sample Input 3
42
Sample Output 3
Positive
-10^9 ≤ N ≤ 10^9
Use if-elif-else to compare N with 0.
2
Largest of Three Numbers
Easy
Given three integers, find and print the largest among them.
Three space-separated integers A, B, C.
Print the largest number.
Sample Input 1
12 45 23
Sample Output 1
45
Sample Input 2
5 9 7
Sample Output 2
9
Sample Input 3
-3 -9 -1
Sample Output 3
-1
-10^6 ≤ A, B, C ≤ 10^6
Use nested if or comparisons to find the maximum.
3
Vowel or Consonant
Easy
Given a single character, check whether it is a vowel (a, e, i, o, u) or a consonant. Assume input is a lowercase letter.
A single lowercase character.
Print "Vowel" if the character is a vowel, otherwise print "Consonant".
Sample Input 1
e
Sample Output 1
Vowel
Sample Input 2
b
Sample Output 2
Consonant
Sample Input 3
z
Sample Output 3
Consonant
Character is a lowercase English letter.
Use if and in operator: if ch in "aeiou".
4
Divisible by 3 and 5
Easy
Is N divisible by both 3 and 5?
Single int N
"Yes" or "No"
Sample Input 1
15
Sample Output 1
Yes
Sample Input 2
30
Sample Output 2
Yes
Sample Input 3
10
Sample Output 3
No
1≤N≤10⁶
N%3==0 and N%5==0
5
Alphabet Check
Easy
Is character an alphabet letter?
Single char
"Alphabet" or "Not an Alphabet"
Sample Input 1
M
Sample Output 1
Alphabet
Sample Input 2
5
Sample Output 2
Not an Alphabet
Sample Input 3
@
Sample Output 3
Not an Alphabet
Printable ASCII
Check A-Z and a-z ranges.
6
Compare Two Numbers
Easy
Print relationship: A>B, A
Two ints A B
Relationship string.
Sample Input 1
7 7
Sample Output 1
A == B
Sample Input 2
5 8
Sample Output 2
A < B
Sample Input 3
8 5
Sample Output 3
A > B
-10⁶≤A,B≤10⁶
if-elif-else
7
Day of Week
Easy
Number 1-7 to day name (1=Sunday).
Single int N (1-7)
Day name.
Sample Input 1
3
Sample Output 1
Tuesday
Sample Input 2
1
Sample Output 2
Sunday
Sample Input 3
7
Sample Output 3
Saturday
1≤N≤7
List or if-elif.
8
Month Days
Easy
Number of days in month M (Feb=28, ignore leap).
Single int M (1-12)
Number of days.
Sample Input 1
2
Sample Output 1
28
Sample Input 2
1
Sample Output 2
31
Sample Input 3
12
Sample Output 3
31
1≤M≤12
if-elif for 31/30/28 months.
9
Uppercase or Lowercase
Easy
Is letter uppercase or lowercase?
Single letter
"Uppercase" or "Lowercase"
Sample Input 1
a
Sample Output 1
Lowercase
Sample Input 2
Z
Sample Output 2
Uppercase
Sample Input 3
m
Sample Output 3
Lowercase
A-Z or a-z
Check range.
10
Profit or Loss
Easy
CP vs SP: profit or loss?
Two ints CP SP
"Profit"/"Loss"/"No Profit No Loss"
Sample Input 1
100 130
Sample Output 1
Profit
Sample Input 2
50 50
Sample Output 2
No Profit No Loss
Sample Input 3
130 100
Sample Output 3
Loss
1≤CP,SP≤10⁶
Compare SP and CP.
1
Grade Calculator
Medium
Given marks in five subjects (out of 100 each), calculate the percentage and assign a grade: A (≥90%), B (≥75%), C (≥50%), D (≥35%), F (
Five space-separated integers representing marks in five subjects.
Print the percentage (rounded to 2 decimal places) on the first line and the grade on the second line.
Sample Input 1
85 92 78 66 91
Sample Output 1
82.40
B
Sample Input 2
40 45 50 55 60
Sample Output 2
50.00
C
Sample Input 3
95 92 88 90 91
Sample Output 3
91.20
A
0 ≤ marks ≤ 100 for each subject.
Calculate total / 5, then use if-elif ladder for grade assignment.
2
Triangle Type
Medium
Given three side lengths, determine the type of triangle: Equilateral (all sides equal), Isosceles (two sides equal), or Scalene (all sides different). Also check if the sides can form a valid triangle (sum of any two sides > third side).
Three space-separated integers A, B, C representing side lengths.
If the sides form a valid triangle, print "Equilateral", "Isosceles", or "Scalene". Otherwise print "Not a Triangle".
Sample Input 1
5 5 8
Sample Output 1
Isosceles
Sample Input 2
6 6 6
Sample Output 2
Equilateral
Sample Input 3
2 3 7
Sample Output 3
Not a Triangle
1 ≤ A, B, C ≤ 10^4
First validate: A + B > C, B + C > A, A + C > B. Then check equality conditions.
3
Leap Year Month Days
Medium
Days in month M of year Y, accounting for leap year.
Two ints Y M
Number of days.
Sample Input 1
2024 2
Sample Output 1
29
Sample Input 2
1900 2
Sample Output 2
28
Sample Input 3
2000 2
Sample Output 3
29
1000≤Y≤3000, 1≤M≤12
Check leap year first.
4
Sort Three Numbers
Medium
Sort 3 ints ascending using conditionals only (no built-in sort).
Three ints A B C
Ascending order.
Sample Input 1
9 5 7
Sample Output 1
5 7 9
Sample Input 2
-5 0 3
Sample Output 2
-5 0 3
Sample Input 3
1 1 1
Sample Output 3
1 1 1
-10⁶≤A,B,C≤10⁶
Compare and swap.
5
Electricity Bill
Medium
Slab: 100@₹5, 200@₹7, beyond@₹10
Single int U (units)
Total bill as int.
Sample Input 1
350
Sample Output 1
2400
Sample Input 2
0
Sample Output 2
0
Sample Input 3
300
Sample Output 3
1900
0≤U≤10000
if-elif slabs.
6
Valid Triangle Angles
Medium
Sum=180 and all>0?
Three ints A B C (degrees)
"Valid" or "Invalid Triangle"
Sample Input 1
60 60 60
Sample Output 1
Valid Triangle
Sample Input 2
0 90 90
Sample Output 2
Invalid Triangle
Sample Input 3
-10 100 90
Sample Output 3
Invalid Triangle
-10⁴≤A,B,C≤10⁴
Check sum==180 and all>0.
7
Divisible by 2&3 but not 5
Medium
Is N divisible by 2 and 3 but not 5?
Single int N
"Yes" or "No"
Sample Input 1
12
Sample Output 1
Yes
Sample Input 2
30
Sample Output 2
No
Sample Input 3
10
Sample Output 3
No
1≤N≤10⁶
Logical AND/OR/NOT.
8
Character Type
Medium
Classify: Digit/Uppercase/Lowercase/Special
Single char
Category name.
Sample Input 1
#
Sample Output 1
Special Character
Sample Input 2
7
Sample Output 2
Digit
Sample Input 3
A
Sample Output 3
Uppercase
Printable ASCII
isdigit/isupper/islower.
9
Discount Calculator
Medium
Single int P (price)
Final price (2 dp).
Sample Input 1
7500
Sample Output 1
6750.00
Sample Input 2
999
Sample Output 2
999.00
Sample Input 3
10000
Sample Output 3
8500.00
1≤P≤10⁷
if-elif discount percentage.
10
Find Middle Number
Medium
Middle of 3 distinct integers (not min or max).
Three distinct ints A B C
Middle number.
Sample Input 1
12 5 8
Sample Output 1
8
Sample Input 2
1 2 3
Sample Output 2
2
Sample Input 3
-5 0 5
Sample Output 3
0
-10⁶≤A,B,C≤10⁶, distinct
(A+B+C)-min-max
1
Quadratic Equation Roots
Hard
Given coefficients a, b, c of a quadratic equation ax² + bx + c = 0, determine the nature of roots and print them. If discriminant D > 0: "Real and Distinct" with roots. If D = 0: "Real and Equal" with the root. If D < 0: "Imaginary".
Three space-separated integers a, b, c.
First line: nature of roots. Second line: the root(s) separated by space (rounded to 2 decimal places). If imaginary, print only "Imaginary".
Sample Input 1
1 -5 6
Sample Output 1
Real and Distinct
3.00 2.00
Sample Input 2
1 -2 1
Sample Output 2
Real and Equal
1.00
Sample Input 3
1 2 5
Sample Output 3
Imaginary
-100 ≤ a, b, c ≤ 100, a ≠ 0
D = b² - 4ac. Roots = (-b ± √D) / 2a. Use math.sqrt() for square root.
2
Date Validator
Hard
Validate DD MM YYYY (leap-aware).
Three ints DD MM YYYY
"Valid Date" or "Invalid Date"
Sample Input 1
29 2 2024
Sample Output 1
Valid Date
Sample Input 2
31 4 2023
Sample Output 2
Invalid Date
Sample Input 3
29 2 1900
Sample Output 3
Invalid Date
1≤DD≤31, 1≤MM≤12, 1000-3000
Check month, day ranges.
3
Calculator with Menu
Hard
Menu: 1.Add 2.Sub 3.Mul 4.Div 5.Mod
Line1: A. Line2: B. Line3: choice
Result. Div: 2 dp.
Sample Input 1
15
4
3
Sample Output 1
60
Sample Input 2
10
5
2
Sample Output 2
5
Sample Input 3
10
3
5
Sample Output 3
1
1≤A,B≤10⁶, B≠0 for div
if-elif 5 options.
4
Zodiac Sign
Hard
Find zodiac from day and month.
Two ints: day month
Zodiac name.
Sample Input 1
21 3
Sample Output 1
Aries
Sample Input 2
19 1
Sample Output 2
Capricorn
Sample Input 3
25 12
Sample Output 3
Capricorn
Valid dates
Nested if-elif ranges.
5
Ticket Pricing
Hard
Base ₹200. Child≤12: 50% off. Senior≥60: 30% off. Weekend: +20%. IMAX: +₹50.
Four inputs: age, weekend(y/n), imax(y/n), count
Total cost int.
Sample Input 1
10
yes
yes
2
Sample Output 1
340
Sample Input 2
30
no
no
1
Sample Output 2
200
Sample Input 3
65
no
yes
1
Sample Output 3
190
1≤age≤120, count 1-10
Apply discounts and surcharges.
6
Roman to Integer
Hard
Convert Roman numeral string to int (IV=4, IX=9, etc.)
Roman numeral string
Integer value.
Sample Input 1
MCMXCV
Sample Output 1
1995
Sample Input 2
IV
Sample Output 2
4
Sample Input 3
XLVIII
Sample Output 3
48
Len≤15
Traverse: if cur
7
Number to Words
Hard
Convert N (1-999) to English words.
Single int N
English words.
Sample Input 1
342
Sample Output 1
three hundred forty-two
Sample Input 2
100
Sample Output 2
one hundred
Sample Input 3
999
Sample Output 3
nine hundred ninety-nine
1≤N≤999
Maps for 1-19, tens 20-90.
8
Age Category
Hard
Infant(0-2), Toddler(3-4), Child(5-12), Teen(13-19), Adult(20-39), Mid(40-59), Senior(60+)
Single int age
Category name.
Sample Input 1
25
Sample Output 1
Adult
Sample Input 2
1
Sample Output 2
Infant
Sample Input 3
70
Sample Output 3
Senior
0≤age≤150
if-elif ladder.
9
Triangle by Angles
Hard
Acute/all90. Validate first.
Three ints A B C (degrees)
"Acute"/"Right"/"Obtuse"/"Invalid"
Sample Input 1
90 45 45
Sample Output 1
Right
Sample Input 2
100 40 40
Sample Output 2
Obtuse
Sample Input 3
150 20 10
Sample Output 3
Obtuse
1≤A,B,C≤360
Validate sum=180 and all>0.
10
Days Between Two Dates
Hard
Calculate the number of days between two dates accounting for leap years.
Two lines each with DD MM YYYY
Print the number of days between them. Assume first date
Sample Input 1
1 1 2024
1 1 2025
Sample Output 1
366
Sample Input 2
1 1 2023
31 12 2023
Sample Output 2
364
Sample Input 3
1 1 2000
1 1 2100
Sample Output 3
36525
1-31, 1-12, 1000-3000
Convert to days from a reference date using month day arrays and leap year logic.
Competitive MCQs — Conditional Statements
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 35
Q1
What is the output of this code?
python
1
2
3
4
if 5 > 3:
    print("Yes")
else:
    print("No")
Correct!
Wrong — correct answer is .
The condition 5 > 3 is True, so the if branch prints "Yes".
Q2
What is the output of this code?
if 5 > 3:
print("Yes")
else:
print("No")
Correct!
Wrong — correct answer is .
The condition 5 > 3 is True, so the if branch prints "Yes".
Q3
Which keyword begins a conditional block?
Correct!
Wrong — correct answer is .
Python uses if (and optionally elif/else) for conditionals.
Q4
What is the output of this code?
python
1
2
3
4
5
6
7
x = 10
if x > 5:
    print("A")
elif x > 8:
    print("B")
else:
    print("C")
Correct!
Wrong — correct answer is .
The first True condition wins; elif is not checked after if matches.
Q5
What is the output of this code?
x = 10
if x > 5:
print("A")
elif x > 8:
print("B")
else:
print("C")
Correct!
Wrong — correct answer is .
The first True condition wins; elif is not checked after if matches.
Q6
How do you write a condition that is true when x is between 1 and 10 inclusive?
Correct!
Wrong — correct answer is .
Both the chained comparison and the and form are valid Python.
Q7
What is the output of this code?
python
1
print("Even" if 4 % 2 == 0 else "Odd")
Correct!
Wrong — correct answer is .
The ternary expression prints "Even" because 4 % 2 == 0 is True.
Q8
What is the output of this code?
print("Even" if 4 % 2 == 0 else "Odd")
Correct!
Wrong — correct answer is .
The ternary expression prints "Even" because 4 % 2 == 0 is True.
Q9
Which of the following is NOT a valid Python conditional?
Correct!
Wrong — correct answer is .
Python uses elif, not "else if".
Q10
What is the output of this code?
python
1
2
3
4
5
x = 0
if x:
    print("T")
else:
    print("F")
Correct!
Wrong — correct answer is .
0 is falsy in Python, so the else branch prints "F".
Q11
What is the output of this code?
x = 0
if x:
print("T")
else:
print("F")
Correct!
Wrong — correct answer is .
0 is falsy in Python, so the else branch prints "F".
Q12
What does an empty if body need to avoid an IndentationError?
Correct!
Wrong — correct answer is .
pass is a no-op that makes an empty block syntactically valid.
Q13
What is the output of this code?
python
1
2
3
4
5
6
7
8
score = 75
if score >= 90:
    grade = "A"
elif score >= 70:
    grade = "B"
else:
    grade = "C"
print(grade)
Correct!
Wrong — correct answer is .
75 >= 90 is False, then 75 >= 70 is True, so grade = "B".
Q14
What is the output of this code?
score = 75
if score >= 90:
grade = "A"
elif score >= 70:
grade = "B"
else:
grade = "C"
print(grade)
Correct!
Wrong — correct answer is .
75 >= 90 is False, then 75 >= 70 is True, so grade = "B".
Q15
Which expression is equivalent to if not (a or b)?
Correct!
Wrong — correct answer is .
De Morgan's law: not (a or b) equals (not a) and (not b).
Q16
What is the output of this code?
x = 3
if x % 2 == 0:
print("Even")
else:
print("Odd")
Correct!
Wrong — correct answer is .
3 % 2 is 1, not 0, so the else branch prints "Odd".
Q17
What is the output of this code?
if 0:
print("A")
elif "":
print("B")
else:
print("C")
Correct!
Wrong — correct answer is .
Both 0 and "" are falsy, so neither branch runs until the else prints "C".
Q18
What is the output of this code?
x = 5
print("Pos" if x > 0 else "Non-pos")
Correct!
Wrong — correct answer is .
The condition x > 0 is True, so the ternary picks "Pos".
Q19
What is the output of this code?
num = 15
if num % 3 == 0 and num % 5 == 0:
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
else:
print("Buzz")
Correct!
Wrong — correct answer is .
15 is divisible by both 3 and 5, so the first condition wins and prints "FizzBuzz".
Q20
Which of these conditions is True when x is NOT between 1 and 10?
Correct!
Wrong — correct answer is .
Negating the range or using OR of the two outside ranges both work.
Q21
What is the output of this code?
if []:
print("Full")
else:
print("Empty")
Correct!
Wrong — correct answer is .
An empty list is falsy in Python, so the else branch prints "Empty".
Q22
What is the output of this code?
flag = True
if flag is True:
print("Yes")
else:
print("No")
Correct!
Wrong — correct answer is .
flag is True compares identity, which holds, so "Yes" prints.
Q23
Which keyword is used for an alternative condition in Python?
Correct!
Wrong — correct answer is .
Python spells it elif; "else if" and elseif are syntax errors.
Q24
What is the output of this code?
x = "hello"
if len(x) > 3:
print("Long")
elif len(x) > 1:
print("Medium")
else:
print("Short")
Correct!
Wrong — correct answer is .
len("hello") is 5, which is greater than 3, so "Long" prints.
Q25
What is the output of this code?
if None:
print("A")
else:
print("B")
Correct!
Wrong — correct answer is .
None is falsy, so the else branch runs and prints "B".
Q26
What is the output of this code?
x = 100
y = 200
m = x if x > y else y
print(m)
Correct!
Wrong — correct answer is .
Since x > y is False, the ternary selects y, which is 200.
Q27
What is the output of this code?
if 5 > 3 and 2 > 1:
print("Both")
else:
print("Neither")
Correct!
Wrong — correct answer is .
Both comparisons are True, so the combined condition is True.
Q28
What is the output of this code?
x = 8
if x in [2, 4, 6, 8]:
print("Even list")
else:
print("Missing")
Correct!
Wrong — correct answer is .
The in operator finds 8 inside the list, so "Even list" prints.
Q29
What happens if you write if x > 5: without an indented body?
Correct!
Wrong — correct answer is .
Python requires an indented block after the colon; a missing block raises IndentationError.
Q30
What is the output of this code?
if "a" in "cat":
print(1)
if "b" in "cat":
print(2)
Correct!
Wrong — correct answer is .
Only the first if is True ("a" is in "cat"), so only 1 prints; these are independent ifs.
Q31
What is the output of this code?
x = 2
if x < 0:
print("neg")
elif x == 0:
print("zero")
elif x > 0:
print("pos")
Correct!
Wrong — correct answer is .
x is 2, so the final elif condition x > 0 is the first True one, printing "pos".
Q32
Which statement checks several equality cases concisely?
Correct!
Wrong — correct answer is .
The match statement provides structural pattern matching since Python 3.10.
Q33
What is the output of this code?
temperature = 30
if temperature > 25:
print("Hot")
elif temperature > 15:
print("Warm")
else:
print("Cold")
Correct!
Wrong — correct answer is .
30 > 25 is True, so the first branch runs and prints "Hot".
Q34
What is the output of this code?
if (5 > 3) == True:
print("Ok")
Correct!
Wrong — correct answer is .
5 > 3 evaluates to True, and True == True is True, so "Ok" prints.
Q35
Which expression tests if x is a positive even number?
Correct!
Wrong — correct answer is .
Both facts must hold: the number is above zero and leaves no remainder on division by 2.