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.