Python Practice Questions

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

Tuple Practice

Python tuples — ordered, immutable sequences for fixed data.

1
Access Tuple Elements by Index
Easy
Write a program to print the element at a given index from a tuple.
First line contains N. Second line contains N elements. Third line contains K (index).
Print element or "Index out of range".
Sample Input 1
5
10 20 30 40 50
2
Sample Output 1
30
Sample Input 2
3
5 6 7
5
Sample Output 2
Index out of range
Sample Input 3
1
apple
0
Sample Output 3
apple
Sample Input 4
4
a b c d
-1
Sample Output 4
Index out of range
1 ≤ N ≤ 100
2
Count Occurrences in a Tuple
Easy
Write a program to count how many times X appears in a tuple.
First line contains N. Second line contains N elements. Third line contains X.
Print the count.
Sample Input 1
7
1 2 3 2 4 2 5
2
Sample Output 1
3
Sample Input 2
4
a b c d
z
Sample Output 2
0
Sample Input 3
5
x x x x x
x
Sample Output 3
5
Sample Input 4
3
-1 0 -1
-1
Sample Output 4
2
1 ≤ N ≤ 100
3
Find Length of a Tuple
Easy
Write a program to find and print the length of a given tuple.
First line contains N. Second line contains N elements.
Print the length.
Sample Input 1
4
10 20 30 40
Sample Output 1
4
Sample Input 2
1
hello
Sample Output 2
1
Sample Input 3
6
a b c d e f
Sample Output 3
6
Sample Input 4
3
1 2 3
Sample Output 4
3
1 ≤ N ≤ 100
4
Concatenate Two Tuples
Easy
Write a program to read two tuples and print their concatenation.
First line N1. Second line N1 elements. Third line N2. Fourth line N2 elements.
Print concatenated tuple as space-separated elements.
Sample Input 1
3
1 2 3
3
4 5 6
Sample Output 1
1 2 3 4 5 6
Sample Input 2
2
a b
1
c
Sample Output 2
a b c
Sample Input 3
1
10
3
20 30 40
Sample Output 3
10 20 30 40
Sample Input 4
3
x y z
2
p q
Sample Output 4
x y z p q
1 ≤ N1, N2 ≤ 50
5
Check Element Exists in Tuple
Easy
Write a program to check if a given element X exists in a tuple.
First line contains N. Second line contains N elements. Third line contains X.
Print "Found" or "Not Found".
Sample Input 1
5
10 20 30 40 50
30
Sample Output 1
Found
Sample Input 2
4
cat dog bird fish
lion
Sample Output 2
Not Found
Sample Input 3
1
42
42
Sample Output 3
Found
Sample Input 4
3
a b c
d
Sample Output 4
Not Found
1 ≤ N ≤ 100
6
Slice a Tuple
Easy
Write a program to slice a tuple from index S to E (exclusive).
First line contains N. Second line contains N elements. Third line contains S and E.
Print sliced elements space-separated. If S≥E, print nothing.
Sample Input 1
6
10 20 30 40 50 60
2 5
Sample Output 1
30 40 50
Sample Input 2
5
10 20 30 40 50
0 5
Sample Output 2
10 20 30 40 50
Sample Input 3
4
a b c d
2 2
Sample Output 3

                      
Sample Input 4
5
a b c d e
2 3
Sample Output 4
c
1 ≤ N ≤ 100
7
Find Maximum in a Tuple
Easy
Write a program to find the maximum element in a tuple of integers.
First line contains N. Second line contains N integers.
Print the maximum.
Sample Input 1
6
45 12 78 3 99 23
Sample Output 1
99
Sample Input 2
3
-10 -5 -20
Sample Output 2
-5
Sample Input 3
1
42
Sample Output 3
42
Sample Input 4
5
100 0 -100 50 75
Sample Output 4
100
1 ≤ N ≤ 100
8
Convert List to Tuple
Easy
Write a program to convert a list of integers into a tuple and print it.
First line contains N. Second line contains N integers.
Print tuple in Python format, e.g. (1, 2, 3).
Sample Input 1
5
1 2 3 4 5
Sample Output 1
(1, 2, 3, 4, 5)
Sample Input 2
3
10 20 30
Sample Output 2
(10, 20, 30)
Sample Input 3
1
7
Sample Output 3
(7,)
Sample Input 4
4
-1 0 1 2
Sample Output 4
(-1, 0, 1, 2)
1 ≤ N ≤ 100
9
Unpack a Tuple
Easy
Write a program to unpack a tuple into variables and print each on a new line.
First line contains N (≤5). Second line contains N elements.
Print each element on a new line.
Sample Input 1
3
10 20 30
Sample Output 1
10
20
30
Sample Input 2
2
hello world
Sample Output 2
hello
world
Sample Input 3
1
42
Sample Output 3
42
Sample Input 4
4
a b c d
Sample Output 4
a
b
c
d
1 ≤ N ≤ 5
10
Count Unique Elements in a Tuple
Easy
Write a program to count the number of unique elements in a tuple.
First line contains N. Second line contains N elements.
Print the number of unique elements.
Sample Input 1
7
1 2 2 3 4 3 5
Sample Output 1
5
Sample Input 2
5
x x x x x
Sample Output 2
1
Sample Input 3
3
a b c
Sample Output 3
3
Sample Input 4
6
1 1 2 2 3 4
Sample Output 4
4
1 ≤ N ≤ 100
1
Find Index of an Element in a Tuple
Medium
Write a program to find the first occurrence index of X in a tuple.
First line contains N. Second line contains N elements. Third line contains X.
Print the index or -1.
Sample Input 1
6
5 10 15 10 20 25
10
Sample Output 1
1
Sample Input 2
4
cat dog bird fish
lion
Sample Output 2
-1
Sample Input 3
5
x x y z x
x
Sample Output 3
0
Sample Input 4
3
a b c
c
Sample Output 4
2
1 ≤ N ≤ 100
2
Tuple Packing and Unpacking
Medium
Write a program that packs N integers into a tuple, then unpacks first and last elements and prints their sum.
First line contains N (≥2). Second line contains N integers.
Print sum of first and last.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
6
Sample Input 2
2
10 20
Sample Output 2
30
Sample Input 3
6
-5 0 1 2 3 6
Sample Output 3
1
Sample Input 4
3
50 0 50
Sample Output 4
100
2 ≤ N ≤ 100
3
Check Consecutive Occurrences
Medium
Write a program to check if element X appears at least K times consecutively in a tuple.
First line contains N. Second line contains N elements. Third line contains X and K.
Print "Yes" or "No".
Sample Input 1
8
1 2 2 2 3 4 2 5
2 3
Sample Output 1
Yes
Sample Input 2
5
a b b c d
b 3
Sample Output 2
No
Sample Input 3
6
x x x x y z
x 4
Sample Output 3
Yes
Sample Input 4
4
1 2 3 4
5 1
Sample Output 4
No
1 ≤ N ≤ 100
4
Tuple of Squares
Medium
Write a program to create a tuple of squares from given integers.
First line contains N. Second line contains N integers.
Print the tuple of squares.
Sample Input 1
4
1 2 3 4
Sample Output 1
(1, 4, 9, 16)
Sample Input 2
3
0 5 10
Sample Output 2
(0, 25, 100)
Sample Input 3
2
-1 1
Sample Output 3
(1, 1)
Sample Input 4
5
2 3 4 5 6
Sample Output 4
(4, 9, 16, 25, 36)
1 ≤ N ≤ 100
5
Sum of Tuple Elements
Medium
Write a program to compute the sum of all integer elements in a tuple.
First line contains N. Second line contains N integers.
Print the sum.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
15
Sample Input 2
3
-5 0 3
Sample Output 2
-2
Sample Input 3
1
42
Sample Output 3
42
Sample Input 4
6
10 -10 20 -20 30 -30
Sample Output 4
0
1 ≤ N ≤ 100
6
Reverse a Tuple
Medium
Write a program to reverse a tuple and print the result.
First line contains N. Second line contains N elements.
Print reversed tuple as space-separated elements.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
5 4 3 2 1
Sample Input 2
3
a b c
Sample Output 2
c b a
Sample Input 3
1
42
Sample Output 3
42
Sample Input 4
4
10 20 30 40
Sample Output 4
40 30 20 10
1 ≤ N ≤ 100
7
Tuple Membership and Count
Medium
Write a program to check if X exists in a tuple and print its count if found.
First line contains N. Second line contains N elements. Third line contains X.
Print "Yes count: K" or "No".
Sample Input 1
7
3 1 4 1 5 1 9
1
Sample Output 1
Yes count: 3
Sample Input 2
4
cat dog bird fish
lion
Sample Output 2
No
Sample Input 3
6
x x x x x x
x
Sample Output 3
Yes count: 6
Sample Input 4
5
a b c b d
b
Sample Output 4
Yes count: 2
1 ≤ N ≤ 100
8
Frequency of Each Element in a Tuple
Medium
Write a program to count the frequency of each unique element in a tuple.
First line contains N. Second line contains N elements.
Print "element: count" per line in order of first appearance.
Sample Input 1
8
1 2 2 3 2 1 3 4
Sample Output 1
1: 2
2: 3
3: 2
4: 1
Sample Input 2
4
x y x y
Sample Output 2
x: 2
y: 2
Sample Input 3
3
a a a
Sample Output 3
a: 3
Sample Input 4
5
5 5 5 6 6
Sample Output 4
5: 3
6: 2
1 ≤ N ≤ 100
9
Tuple Comparison (Lexicographic)
Medium
Write a program to compare two tuples lexicographically and print which is greater.
First line N1. Second line N1 elements. Third line N2. Fourth line N2 elements.
Print "First is greater", "Second is greater", or "Equal".
Sample Input 1
3
5 1 3
3
1 2 3
Sample Output 1
First is greater
Sample Input 2
3
1 2 3
3
1 3 0
Sample Output 2
Second is greater
Sample Input 3
4
1 2 3 4
4
1 2 3 4
Sample Output 3
Equal
Sample Input 4
3
1 2 3
4
1 2 3 4
Sample Output 4
Second is greater
1 ≤ N1, N2 ≤ 100
10
Min and Max in a Tuple
Medium
Write a program to find the smallest and largest integer in a tuple.
First line contains N. Second line contains N integers.
Print "Min: X Max: Y".
Sample Input 1
6
45 12 78 3 99 23
Sample Output 1
Min: 3 Max: 99
Sample Input 2
5
-10 0 50 -5 25
Sample Output 2
Min: -10 Max: 50
Sample Input 3
1
42
Sample Output 3
Min: 42 Max: 42
Sample Input 4
4
-50 -100 -10 -1
Sample Output 4
Min: -100 Max: -1
1 ≤ N ≤ 100
1
Nested Tuple Element Access
Hard
Write a program to access a deeply nested element in a tuple of tuples given a list of indices (depth path). Assume the nested tuple is: (1, (2, 3), ((4, (5, 6), 7), 8), 9).
First line contains D (depth). Second line contains D space-separated indices (0-based).
Print the element found at the path, or "Index out of range".
Sample Input 1
2
1 2
Sample Output 1
3
Sample Input 2
2
2 0
Sample Output 2
(4, (5, 6), 7)
Sample Input 3
3
2 1 1
Sample Output 3
6
Sample Input 4
2
0 0
Sample Output 4
Index out of range
1 ≤ D ≤ 5
2
Tuple Rotation
Hard
Write a program to rotate a tuple to the left by K positions. Elements at the front wrap around to the end.
First line contains N. Second line contains N elements. Third line contains K (0 ≤ K ≤ 10⁶).
Print the rotated tuple as space-separated elements.
Sample Input 1
7
1 2 3 4 5 6 7
2
Sample Output 1
3 4 5 6 7 1 2
Sample Input 2
4
1 2 3 4
4
Sample Output 2
1 2 3 4
Sample Input 3
3
a b c
5
Sample Output 3
c a b
Sample Input 4
5
10 20 30 40 50
0
Sample Output 4
10 20 30 40 50
1 ≤ N ≤ 100, 0 ≤ K ≤ 10⁶
3
Unique Pairs from Two Tuples
Hard
Write a program to find all unique pairs (a,b) where a is from tuple A and b is from tuple B (Cartesian product without duplicate pairs).
First line N1 elements of A. Third line N2 elements of B.
Print each pair as (a, b) on a new line.
Sample Input 1
2
1 2
2
x y
Sample Output 1
(1, x)
(1, y)
(2, x)
(2, y)
Sample Input 2
1
x
1
p
Sample Output 2
(x, p)
Sample Input 3
2
a b
2
p q
Sample Output 3
(a, p)
(a, q)
(b, p)
(b, q)
Sample Input 4
3
1 2 3
2
0 0
Sample Output 4
(1, 0)
(1, 0)
(2, 0)
(2, 0)
(3, 0)
(3, 0)
1 ≤ N1, N2 ≤ 100
4
Check if Tuple is Monotonic
Hard
Write a program to check if a tuple of integers is strictly increasing, strictly decreasing, or neither.
First line contains N. Second line contains N integers.
Print "Increasing", "Decreasing", or "Neither".
Sample Input 1
5
1 2 3 4 5
Sample Output 1
Increasing
Sample Input 2
5
9 7 5 3 1
Sample Output 2
Decreasing
Sample Input 3
5
1 3 2 5 4
Sample Output 3
Neither
Sample Input 4
2
5 10
Sample Output 4
Increasing
1 ≤ N ≤ 100
5
Most Frequent Element in a Tuple
Hard
Write a program to find the element that appears most frequently in a tuple. If tied, print the smallest element.
First line contains N. Second line contains N integers.
Print the most frequent element (smallest if tied).
Sample Input 1
9
3 1 4 1 5 3 1 9 3
Sample Output 1
1
Sample Input 2
5
5 5 5 2 2
Sample Output 2
5
Sample Input 3
8
-1 -1 0 0 1 1 2 2
Sample Output 3
-1
Sample Input 4
3
7 7 7
Sample Output 4
7
1 ≤ N ≤ 100
6
Tuple Comprehension — Even Squares
Hard
Write a program to create a tuple of squares of even numbers from 1 to N.
First line contains N (1 ≤ N ≤ 50).
Print the tuple of even squares.
Sample Input 1
10
Sample Output 1
(4, 16, 36, 64, 100)
Sample Input 2
5
Sample Output 2
(4, 16)
Sample Input 3
1
Sample Output 3
()
Sample Input 4
8
Sample Output 4
(4, 16, 36, 64)
1 ≤ N ≤ 50
7
Remove Duplicates from Tuple
Hard
Write a program to remove duplicate elements from a tuple while preserving order of first occurrence.
First line contains N. Second line contains N elements.
Print the tuple without duplicates as space-separated elements.
Sample Input 1
8
1 2 2 3 1 4 2 5
Sample Output 1
1 2 3 4 5
Sample Input 2
5
a b a c b
Sample Output 2
a b c
Sample Input 3
4
x x x x
Sample Output 3
x
Sample Input 4
6
1 1 1 1 1 1
Sample Output 4
1
1 ≤ N ≤ 100
8
Tuple of Primes Up to N
Hard
Write a program to generate a tuple of all prime numbers up to N.
First line contains N (2 ≤ N ≤ 100).
Print the tuple of primes as space-separated elements.
Sample Input 1
10
Sample Output 1
2 3 5 7
Sample Input 2
20
Sample Output 2
2 3 5 7 11 13 17 19
Sample Input 3
5
Sample Output 3
2 3 5
Sample Input 4
2
Sample Output 4
2
2 ≤ N ≤ 100
9
Cyclic Pairwise Sum in Tuple
Hard
Write a program to compute the sum of each adjacent pair in a tuple (including last+first as a cycle).
First line contains N (≥2). Second line contains N integers.
Print the tuple of sums as space-separated values.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
3 5 7 9 6
Sample Input 2
3
10 20 30
Sample Output 2
30 50 40
Sample Input 3
2
5 10
Sample Output 3
15 15
Sample Input 4
4
-1 0 1 2
Sample Output 4
-1 1 3 1
2 ≤ N ≤ 100
10
Kth Largest Element in a Tuple
Hard
Write a program to find the K-th largest element in a tuple of distinct integers.
First line contains N and K. Second line contains N distinct integers.
Print the K-th largest element.
Sample Input 1
6 3
10 5 8 20 3 15
Sample Output 1
10
Sample Input 2
5 1
100 50 75 25 200
Sample Output 2
200
Sample Input 3
4 4
1 2 3 4
Sample Output 3
1
Sample Input 4
5 2
-5 -10 0 10 5
Sample Output 4
5
1 ≤ K ≤ N ≤ 100