Python Practice Questions

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

Operators Practice

Arithmetic, comparison, logical, assignment, bitwise, and membership operators in Python.

1
Last Digit of a Number
Easy
Given an integer N, print its last digit (units digit).
A single integer N.
Print the last digit of N.
Sample Input 1
237
Sample Output 1
7
Sample Input 2
0
Sample Output 2
0
Sample Input 3
1000000000
Sample Output 3
0
0 ≤ N ≤ 10^9
Use the modulo operator: N % 10 gives the last digit.
2
Check Divisibility
Easy
Given two integers A and B, check if A is divisible by B.
Two space-separated integers A and B.
Print "Divisible" if A is divisible by B, otherwise print "Not Divisible".
Sample Input 1
15 4
Sample Output 1
Not Divisible
Sample Input 2
20 5
Sample Output 2
Divisible
Sample Input 3
7 2
Sample Output 3
Not Divisible
1 ≤ A, B ≤ 10^6
Use the modulo operator %: if A % B == 0 then A is divisible by B.
3
Find the Remainder
Easy
Given two integers A and B, find the remainder when A is divided by B.
Two space-separated integers A and B.
Print the remainder as an integer.
Sample Input 1
17 5
Sample Output 1
2
Sample Input 2
100 7
Sample Output 2
2
Sample Input 3
5 2
Sample Output 3
1
1 ≤ A, B ≤ 10^6
Use the modulo operator: A % B.
4
Average of Three Numbers
Easy
Compute average of three numbers.
Three ints A B C
Average rounded to 2 dp.
Sample Input 1
10 20 30
Sample Output 1
20.00
Sample Input 2
5 5 5
Sample Output 2
5.00
Sample Input 3
-1 0 1
Sample Output 3
0.00
1≤A,B,C≤10⁶
Sum/3
5
Check Odd or Even
Easy
Use modulo operator.
Single int N
"Even" or "Odd"
Sample Input 1
7
Sample Output 1
Odd
Sample Input 2
0
Sample Output 2
Even
Sample Input 3
1000000000
Sample Output 3
Even
1≤N≤10⁹
N%2==0 means even.
6
Power of a Number
Easy
Compute B^E using operator.
Two ints B E
Print B^E result.
Sample Input 1
2 10
Sample Output 1
1024
Sample Input 2
3 3
Sample Output 2
27
Sample Input 3
10 0
Sample Output 3
1
1≤B≤10, 0≤E≤15
Use ** operator.
7
Floor Division
Easy
Integer division A//B
Two ints A B
Print A//B.
Sample Input 1
25 4
Sample Output 1
6
Sample Input 2
100 7
Sample Output 2
14
Sample Input 3
5 5
Sample Output 3
1
1≤B≤A≤10⁶
Use //
8
Increment and Decrement
Easy
Print N+1 and N-1 using += and -=.
Single int N
Two lines: N+1, N-1
Sample Input 1
10
Sample Output 1
11
9
Sample Input 2
0
Sample Output 2
1
-1
Sample Input 3
-1000000
Sample Output 3
-999999
-1000001
-10⁶≤N≤10⁶
Use += and -=
9
Check Greater
Easy
Is A > B using > operator?
Two ints A B
"Yes" or "No"
Sample Input 1
15 10
Sample Output 1
Yes
Sample Input 2
5 5
Sample Output 2
No
Sample Input 3
-1 1
Sample Output 3
No
-10⁶≤A,B≤10⁶
Use >
10
Tens Digit
Easy
Extract tens place digit using // and %.
Single int N
Print tens digit.
Sample Input 1
345
Sample Output 1
4
Sample Input 2
100
Sample Output 2
0
Sample Input 3
1000000
Sample Output 3
0
10≤N≤10⁶
(N//10)%10
1
Check Leap Year with Logical Operators
Medium
Given a year Y, determine if it is a leap year using logical operators. A year is a leap year if it is divisible by 400 OR (divisible by 4 AND not divisible by 100).
A single integer Y.
Print "Leap Year" or "Not a Leap Year".
Sample Input 1
1900
Sample Output 1
Not a Leap Year
Sample Input 2
2000
Sample Output 2
Leap Year
Sample Input 3
2024
Sample Output 3
Leap Year
1000 ≤ Y ≤ 3000
Use logical operators: Y % 400 == 0 or (Y % 4 == 0 and Y % 100 != 0)
2
Swap Bits (Bitwise XOR)
Medium
Given two integers X and Y, swap their values without using a third variable. Use only bitwise XOR operators.
Two space-separated integers X and Y.
Print the swapped values in the format: "X = [newX], Y = [newY]"
Sample Input 1
9 14
Sample Output 1
X = 14, Y = 9
Sample Input 2
5 12
Sample Output 2
X = 12, Y = 5
Sample Input 3
100 57
Sample Output 3
X = 57, Y = 100
1 ≤ X, Y ≤ 10^6
X = X ^ Y; Y = X ^ Y; X = X ^ Y. XOR has the property that a ^ b ^ b = a.
3
Power of Two Check
Medium
Is N a power of 2? Use (N & (N-1))==0
Single int N
"Power of Two" or "Not"
Sample Input 1
16
Sample Output 1
Power of Two
Sample Input 2
1
Sample Output 2
Power of Two
Sample Input 3
100
Sample Output 3
Not
1≤N≤10⁹
Use bitwise AND.
4
Evaluate Expression
Medium
(A+B)*(C-D)/E with floats
Five ints A B C D E
Result rounded to 2 dp.
Sample Input 1
10 5 20 7 4
Sample Output 1
48.75
Sample Input 2
1 1 5 2 2
Sample Output 2
3.00
Sample Input 3
3 4 6 2 10
Sample Output 3
2.80
C>D, E≠0
Follow precedence.
5
Nth Bit Check
Medium
Check if K-th bit of N is set using (1
Two ints N K
"Set" or "Not Set"
Sample Input 1
13 2
Sample Output 1
Set
Sample Input 2
13 3
Sample Output 2
Set
Sample Input 3
0 0
Sample Output 3
Not Set
0≤N≤10⁹, 0≤K≤31
N & (1
6
Toggle Bits
Medium
Toggle all bits after MSB using XOR.
Single int N
Result integer.
Sample Input 1
10
Sample Output 1
5
Sample Input 2
1
Sample Output 2
0
Sample Input 3
31
Sample Output 3
0
1≤N≤10⁶
Mask and XOR.
7
Right Shift Count
Medium
Count right shifts needed to reach 0 (N>>1).
Single int N
Count of shifts.
Sample Input 1
8
Sample Output 1
4
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1024
Sample Output 3
11
1≤N≤10⁹
Count till N==0.
8
Absolute Value
Medium
Absolute value without abs(), using conditionals.
Single int N
Print absolute value.
Sample Input 1
-15
Sample Output 1
15
Sample Input 2
0
Sample Output 2
0
Sample Input 3
1000000000
Sample Output 3
1000000000
-10⁹≤N≤10⁹
If N
9
Same Sign Check
Medium
Do A and B have same sign?
Two ints A B
"Same Sign" or "Different Sign"
Sample Input 1
-5 -10
Sample Output 1
Same Sign
Sample Input 2
0 0
Sample Output 2
Same Sign
Sample Input 3
-1 1
Sample Output 3
Different Sign
-10⁶≤A,B≤10⁶
Check >=0 conditions.
10
BMI Calculator
Medium
BMI = weight / height²
Two floats: weight(kg) height(m)
BMI rounded 2 dp.
Sample Input 1
72 1.75
Sample Output 1
23.51
Sample Input 2
100 2.0
Sample Output 2
25.00
Sample Input 3
50 1.6
Sample Output 3
19.53
20≤w≤200, 1≤h≤2.5
Use ** for square.
1
Count Set Bits
Hard
Given an integer N, count the number of 1 bits (set bits) in its binary representation.
A single integer N.
Print the count of set bits in the binary representation of N.
Sample Input 1
13
Sample Output 1
3
Sample Input 2
255
Sample Output 2
8
Sample Input 3
7
Sample Output 3
3
1 ≤ N ≤ 10^9
Binary of 13 is 1101, which has three 1s. Use N & (N-1) trick or bitwise shift.
2
Gray Code Converter
Hard
Convert binary string to Gray code (XOR each bit with previous).
Binary string (0s/1s)
Gray code binary string.
Sample Input 1
1011
Sample Output 1
1110
Sample Input 2
1000
Sample Output 2
1100
Sample Input 3
1111
Sample Output 3
1000
Len≤32
MSB same, Gray[i]=bin[i]^bin[i-1]
3
Missing Number XOR
Hard
Find missing number from 1..N using XOR.
Line1: N. Line2: N-1 ints
Print missing number.
Sample Input 1
5
3 1 5 2
Sample Output 1
4
Sample Input 2
3
1 2
Sample Output 2
3
Sample Input 3
4
4 3 2
Sample Output 3
1
2≤N≤10⁵
XOR all 1..N with array.
4
Postfix Evaluation
Hard
Evaluate RPN expression (+,-,*,/). Single-digit operands.
Space-separated postfix.
Integer result.
Sample Input 1
5 3 + 8 * 4 /
Sample Output 1
16
Sample Input 2
3 4 + 2 *
Sample Output 2
14
Sample Input 3
8 2 / 4 *
Sample Output 3
16
Expr≤100 chars
Stack-based.
5
Two Unique Numbers
Hard
Array where two numbers appear once, rest twice. Find them using XOR.
Line1: N. Line2: N ints
Two unique numbers asc.
Sample Input 1
8
1 2 3 2 1 4 5 5
Sample Output 1
3 4
Sample Input 2
6
1 1 2 2 3 4
Sample Output 2
3 4
Sample Input 3
4
5 6 5 6
Sample Output 3
0 0
2≤N≤10⁵
XOR all -> find set bit, partition.
6
Max AND in Range
Hard
Max value of A AND B for L≤A
Two ints L R
Max AND value.
Sample Input 1
2 8
Sample Output 1
6
Sample Input 2
5 7
Sample Output 2
6
Sample Input 3
8 16
Sample Output 3
14
1≤L
Find MSB where L and R differ.
7
Triplet Unique
Hard
Every element appears 3x except one. Find it using bitwise.
Line1: N. Line2: N ints
Print unique element.
Sample Input 1
10
3 5 3 3 2 5 5 7 2 2
Sample Output 1
7
Sample Input 2
7
1 1 1 2 2 2 3
Sample Output 2
3
Sample Input 3
4
10 10 10 5
Sample Output 3
5
4≤N≤10⁵
ones/twos bit tracking.
8
Square Root Binary
Hard
Floor sqrt(N) without sqrt() or **0.5, using binary search.
Single int N
Floor of sqrt.
Sample Input 1
50
Sample Output 1
7
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1000000000
Sample Output 3
31622
1≤N≤10⁹
Binary search with *.
9
Bit Palindrome
Hard
Is binary representation a palindrome?
Single int N
"Bit Palindrome" or "Not"
Sample Input 1
9
Sample Output 1
Bit Palindrome
Sample Input 2
5
Sample Output 2
Bit Palindrome
Sample Input 3
12
Sample Output 3
Not
1≤N≤10⁶
Extract bits, compare.
10
Next Power of 2
Hard
Smallest power of 2 ≥ N using bitwise.
Single int N
Next power of 2.
Sample Input 1
25
Sample Output 1
32
Sample Input 2
1
Sample Output 2
1
Sample Input 3
1000000000
Sample Output 3
1073741824
1≤N≤10⁹
Decrement, set bits via OR/shifts.