Python Practice Questions

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

Loops Practice

for loops, while loops, break, continue, and else clauses in Python.

1
Print 1 to N
Easy
Given an integer N, print all numbers from 1 to N on separate lines.
A single integer N.
Print numbers from 1 to N, each on a new line.
Sample Input 1
5
Sample Output 1
1
2
3
4
5
Sample Input 2
1
Sample Output 2
1
Sample Input 3
10
Sample Output 3
1
2
3
4
5
6
7
8
9
10
1 ≤ N ≤ 1000
Use a for loop with range(1, N+1).
2
Sum of N Natural Numbers
Easy
Given an integer N, find the sum of the first N natural numbers.
A single integer N.
Print the sum as an integer.
Sample Input 1
10
Sample Output 1
55
Sample Input 2
100
Sample Output 2
5050
Sample Input 3
1
Sample Output 3
1
1 ≤ N ≤ 10^6
Use a for loop to accumulate, or the formula N*(N+1)//2
3
Multiplication Table
Easy
Given an integer N, print its multiplication table from 1 to 10.
A single integer N.
Print 10 lines in format: "N x i = result" for i from 1 to 10.
Sample Input 1
7
Sample Output 1
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Sample Input 2
3
Sample Output 2
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Sample Input 3
12
Sample Output 3
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
1 ≤ N ≤ 100
Loop i from 1 to 10 and print f"{N} x {i} = {N*i}"
4
Even Numbers up to N
Easy
Print all even numbers 2 to N.
Single int N
Space-separated evens.
Sample Input 1
10
Sample Output 1
2 4 6 8 10
Sample Input 2
1
Sample Output 2

                      
Sample Input 3
20
Sample Output 3
2 4 6 8 10 12 14 16 18 20
1≤N≤1000
range(2,N+1,2)
5
Sum of Even Numbers
Easy
Sum of even numbers 2 to N.
Single int N
Sum int.
Sample Input 1
10
Sample Output 1
30
Sample Input 2
1
Sample Output 2
0
Sample Input 3
1000000
Sample Output 3
250000500000
1≤N≤10⁶
Step 2 loop.
6
Print in Reverse
Easy
Numbers N down to 1.
Single int N
Space-separated.
Sample Input 1
5
Sample Output 1
5 4 3 2 1
Sample Input 2
1
Sample Output 2
1
Sample Input 3
10
Sample Output 3
10 9 8 7 6 5 4 3 2 1
1≤N≤1000
range(N,0,-1)
7
Count Digits
Easy
Count digits using while loop (N//=10).
Single int N
Digit count.
Sample Input 1
98765
Sample Output 1
5
Sample Input 2
1000000000
Sample Output 2
10
Sample Input 3
5
Sample Output 3
1
1≤N≤10⁹
while N>0: count++, N//=10
8
Sum of Odd Numbers
Easy
Sum of odd numbers 1 to N.
Single int N
Sum int.
Sample Input 1
9
Sample Output 1
25
Sample Input 2
2
Sample Output 2
1
Sample Input 3
1000000
Sample Output 3
250000000000
1≤N≤10⁶
Step 2 from 1.
9
Print Characters
Easy
Each character of string on new line.
Single line string
Each char per line.
Sample Input 1
Python
Sample Output 1
P
y
t
h
o
n
Sample Input 2
Hi
Sample Output 2
H
i
Sample Input 3
ABC
Sample Output 3
A
B
C
1≤|S|≤100
for ch in S: print(ch)
10
Reverse Table
Easy
Multiplication table 10 down to 1.
Single int N
10 lines N×i=result.
Sample Input 1
6
Sample Output 1
6×10=60
6×9=54
6×8=48
6×7=42
6×6=36
6×5=30
6×4=24
6×3=18
6×2=12
6×1=6
Sample Input 2
3
Sample Output 2
3×10=30
3×9=27
3×8=24
3×7=21
3×6=18
3×5=15
3×4=12
3×3=9
3×2=6
3×1=3
Sample Input 3
10
Sample Output 3
10×10=100
10×9=90
10×8=80
10×7=70
10×6=60
10×5=50
10×4=40
10×3=30
10×2=20
10×1=10
1≤N≤100
range(10,0,-1)
1
Factorial of a Number
Medium
Given a non-negative integer N, compute its factorial N! = N × (N-1) × (N-2) × ... × 1. Factorial of 0 is 1.
A single integer N.
Print the factorial as an integer.
Sample Input 1
6
Sample Output 1
720
Sample Input 2
0
Sample Output 2
1
Sample Input 3
10
Sample Output 3
3628800
0 ≤ N ≤ 20
Initialize result = 1, loop from 1 to N multiplying step by step.
2
Fibonacci Series
Medium
Generate the first N terms of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, ... where each term is the sum of the previous two.
A single integer N (number of terms).
Print N space-separated Fibonacci numbers.
Sample Input 1
8
Sample Output 1
0 1 1 2 3 5 8 13
Sample Input 2
1
Sample Output 2
0
Sample Input 3
10
Sample Output 3
0 1 1 2 3 5 8 13 21 34
1 ≤ N ≤ 50
Start with a=0, b=1. In a loop, print a, then update a,b = b, a+b.
3
Prime Number Checker
Medium
Given an integer N, determine if it is a prime number (divisible only by 1 and itself).
A single integer N.
Print "Prime" if N is prime, otherwise print "Not Prime".
Sample Input 1
29
Sample Output 1
Prime
Sample Input 2
2
Sample Output 2
Prime
Sample Input 3
49
Sample Output 3
Not Prime
1 ≤ N ≤ 10^6
Loop from 2 to sqrt(N). If N % i == 0 for any i, it is not prime.
4
Perfect Number Loop
Medium
Sum of proper divisors equals N?
Single int N
"Perfect" or "Not Perfect"
Sample Input 1
496
Sample Output 1
Perfect
Sample Input 2
28
Sample Output 2
Perfect
Sample Input 3
12
Sample Output 3
Not Perfect
1≤N≤10000
Loop 1..N//2, sum.
5
Sum of Squares
Medium
1²+2²+...+N²
Single int N
Sum int.
Sample Input 1
5
Sample Output 1
55
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1000
Sample Output 3
333833500
1≤N≤1000
sum += i*i
6
Palindromic Number Count
Medium
Count palindromes between A and B inclusive.
Two ints A B
Count.
Sample Input 1
10 50
Sample Output 1
4
Sample Input 2
1 9
Sample Output 2
9
Sample Input 3
100 120
Sample Output 3
2
1≤A≤B≤10⁵
str(n)==str(n)[::-1]
7
Harshad Number
Medium
N divisible by sum of its digits.
Single int N
"Harshad" or "Not"
Sample Input 1
18
Sample Output 1
Harshad Number
Sample Input 2
21
Sample Output 2
Harshad Number
Sample Input 3
15
Sample Output 3
Not
1≤N≤10⁶
Sum digits, check N%sum==0.
8
Find Factors
Medium
All factors of N ascending.
Single int N
Space-separated factors.
Sample Input 1
36
Sample Output 1
1 2 3 4 6 9 12 18 36
Sample Input 2
1
Sample Output 2
1
Sample Input 3
100
Sample Output 3
1 2 4 5 10 20 25 50 100
1≤N≤10⁶
1..N, N%i==0
9
Decimal to Binary
Medium
Convert N to binary using repeated division by 2.
Single int N
Binary (no leading zeros).
Sample Input 1
25
Sample Output 1
11001
Sample Input 2
1000000
Sample Output 2
11110100001001000000
Sample Input 3
7
Sample Output 3
111
1≤N≤10⁶
while N>0: prepend N%2
10
LCM via Loop
Medium
Smallest number divisible by both A and B.
Two ints A B
LCM int.
Sample Input 1
15 20
Sample Output 1
60
Sample Input 2
7 11
Sample Output 2
77
Sample Input 3
4 6
Sample Output 3
12
1≤A,B≤10⁴
Start from max(A,B), increment.
1
Armstrong Number Check
Hard
An Armstrong number (also called narcissistic number) is a number that equals the sum of its own digits each raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³. Check if a given number is an Armstrong number.
A single integer N.
Print "Armstrong Number" if N is an Armstrong number, otherwise print "Not an Armstrong Number".
Sample Input 1
153
Sample Output 1
Armstrong Number
Sample Input 2
9474
Sample Output 2
Armstrong Number
Sample Input 3
123
Sample Output 3
Not an Armstrong Number
1 ≤ N ≤ 10^7
Count digits first, then extract each digit, raise to power of digit count, and sum.
2
Count Digits, Letters, and Special Characters
Hard
Given a string containing a mix of characters, count the number of digits, alphabetic letters, and special characters in it.
A single line containing a string S.
Print three lines: "Digits: [count]", "Letters: [count]", "Specials: [count]"
Sample Input 1
Hello123!@#
Sample Output 1
Digits: 3
Letters: 5
Specials: 3
Sample Input 2
Abc123$%^
Sample Output 2
Digits: 3
Letters: 3
Specials: 3
Sample Input 3
No digits here
Sample Output 3
Digits: 0
Letters: 12
Specials: 2
1 ≤ |S| ≤ 1000
Loop through each character. Use .isdigit(), .isalpha() methods.
3
Circular Prime
Hard
N and all its digit rotations are prime.
Single int N
"Circular Prime" or "Not"
Sample Input 1
197
Sample Output 1
Circular Prime
Sample Input 2
13
Sample Output 2
Circular Prime
Sample Input 3
23
Sample Output 3
Not
10≤N≤10⁶
Rotate digits, check prime each.
4
Ugly Number
Hard
Only prime factors 2,3,5.
Single int N
"Ugly Number" or "Not"
Sample Input 1
14
Sample Output 1
Not an Ugly Number
Sample Input 2
1
Sample Output 2
Ugly Number
Sample Input 3
30
Sample Output 3
Ugly Number
1≤N≤10⁶
Divide by 2,3,5 until can't.
5
Happy Number
Hard
Sum of squares of digits → 1 or cycle. Detect cycle with set.
Single int N
"Happy" or "Unhappy"
Sample Input 1
19
Sample Output 1
Happy Number
Sample Input 2
2
Sample Output 2
Unhappy
Sample Input 3
4
Sample Output 3
Unhappy
1≤N≤10⁶
Set to detect cycles.
6
Disarium Numbers
Hard
Sum of digits^position equals number. Find between A and B.
Two ints A B
Space-separated or "None".
Sample Input 1
1 100
Sample Output 1
1 2 3 4 5 6 7 8 9 89
Sample Input 2
1 10
Sample Output 2
1 2 3 4 5 6 7 8 9
Sample Input 3
90 100
Sample Output 3
None
1≤A≤B≤10000
Extract digits, sum powers.
7
Smith Number
Hard
Composite with sum-of-digits = sum-of-digits of prime factors.
Single int N
"Smith Number" or "Not"
Sample Input 1
666
Sample Output 1
Smith Number
Sample Input 2
85
Sample Output 2
Smith Number
Sample Input 3
22
Sample Output 3
Smith Number
4≤N≤10⁵
Find prime factors, sum digits.
8
Kaprekar Constant
Hard
4-digit N reaches 6174. Count iterations (desc-asc).
Single 4-digit N (not all same)
Iteration count.
Sample Input 1
3524
Sample Output 1
3
Sample Input 2
1000
Sample Output 2
5
Sample Input 3
6174
Sample Output 3
0
1000-9999, not identical
Subtract asc from desc, repeat.
9
Floyd's Triangle
Hard
First N rows: sequential numbers in right triangle.
Single int N
N rows, row i has i numbers.
Sample Input 1
5
Sample Output 1
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Sample Input 2
1
Sample Output 2
1
Sample Input 3
3
Sample Output 3
1
2 3
4 5 6
1≤N≤20
Counter + nested loops.
10
Neon Number
Hard
Sum of digits of N² equals N.
Single int N
"Neon Number" or "Not"
Sample Input 1
9
Sample Output 1
Neon Number
Sample Input 2
1
Sample Output 2
Neon Number
Sample Input 3
45
Sample Output 3
Not
1≤N≤10⁴
N*N, sum digits, compare.