Python Practice Questions

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

Set Practice

Python sets — unordered collections of unique elements with mathematical set operations.

1
Create a Set from a List
Easy
Write a program to create a set from a list of integers and print unique elements sorted.
First line contains N. Second line contains N integers.
Print unique elements sorted ascending.
Sample Input 1
7
1 2 2 3 4 3 5
Sample Output 1
1 2 3 4 5
Sample Input 2
4
1 2 3 4
Sample Output 2
1 2 3 4
Sample Input 3
5
10 10 10 10 10
Sample Output 3
10
Sample Input 4
6
1 -2 0 1 2 -1
Sample Output 4
-2 -1 0 1 2
1 ≤ N ≤ 100
2
Add Elements to a Set
Easy
Write a program to add K elements to an existing set and print final set sorted.
First line N. Second line N initial elements. Third line K. Fourth line K new elements.
Print final sorted set.
Sample Input 1
3
1 2 3
2
4 5
Sample Output 1
1 2 3 4 5
Sample Input 2
4
1 2 3 4
3
1 2 5
Sample Output 2
1 2 3 4 5
Sample Input 3
2
a b
1
c
Sample Output 3
a b c
Sample Input 4
3
10 20 30
2
10 30
Sample Output 4
10 20 30
1 ≤ N, K ≤ 50
3
Remove Element from Set
Easy
Write a program to remove element X from a set.
First line N. Second line N elements. Third line X.
Print "Removed" or "Not found".
Sample Input 1
5
1 2 3 4 5
3
Sample Output 1
Removed
Sample Input 2
4
a b c d
z
Sample Output 2
Not found
Sample Input 3
3
x x x
x
Sample Output 3
Removed
Sample Input 4
6
10 20 30 40 50
5
Sample Output 4
Not found
1 ≤ N ≤ 100
4
Check Membership in Set
Easy
Write a program to check if element X exists in a set.
First line N. Second line N elements. Third line X.
Print "Yes" or "No".
Sample Input 1
5
5 10 15 20 25
15
Sample Output 1
Yes
Sample Input 2
4
cat dog bird fish
lion
Sample Output 2
No
Sample Input 3
3
apple banana cherry
banana
Sample Output 3
Yes
Sample Input 4
5
1 2 3 4 5
6
Sample Output 4
No
1 ≤ N ≤ 100
5
Size of a Set
Easy
Write a program to find and print the number of unique elements in a set.
First line N. Second line N elements.
Print size of set.
Sample Input 1
8
3 1 4 1 5 9 2 6
Sample Output 1
7
Sample Input 2
5
x x x x x
Sample Output 2
1
Sample Input 3
4
a b c d
Sample Output 3
4
Sample Input 4
6
1 1 2 2 3 3
Sample Output 4
3
1 ≤ N ≤ 100
6
Convert Set to Sorted List
Easy
Write a program to convert a set into a sorted list.
First line N. Second line N elements.
Print unique elements sorted.
Sample Input 1
7
3 1 4 1 5 2 3
Sample Output 1
1 2 3 4 5
Sample Input 2
5
50 10 30 20 40
Sample Output 2
10 20 30 40 50
Sample Input 3
3
c a b
Sample Output 3
a b c
Sample Input 4
6
3 10 -5 0 8 8
Sample Output 4
-5 0 3 8 10
1 ≤ N ≤ 100
7
Iterate Over a Set
Easy
Write a program to iterate over a set and print each element on a new line.
First line N. Second line N elements.
Print each unique element on a new line.
Sample Input 1
5
1 2 3 4 5
Sample Output 1
1
2
3
4
5
Sample Input 2
4
cat dog bird fish
Sample Output 2
cat
dog
bird
fish
Sample Input 3
3
a b a c
Sample Output 3
a
b
c
Sample Input 4
4
10 20 20 10
Sample Output 4
10
20
1 ≤ N ≤ 100
8
Clear a Set
Easy
Write a program to clear all elements from a set and show it is empty.
First line N. Second line N elements.
Print "set()".
Sample Input 1
5
1 2 3 4 5
Sample Output 1
set()
Sample Input 2
3
a b c
Sample Output 2
set()
Sample Input 3
4
10 20 30 40
Sample Output 3
set()
Sample Input 4
2
x y
Sample Output 4
set()
1 ≤ N ≤ 100
9
Pop from a Set
Easy
Write a program to remove and return an arbitrary element from a set. If empty, print "Empty set".
First line N. Second line N elements.
Print popped element or "Empty set".
Sample Input 1
3
5 10 15
Sample Output 1
Popped
Sample Input 2
0
Sample Output 2
Empty set
Sample Input 3
1
42
Sample Output 3
42
Sample Input 4
4
a b c d
Sample Output 4
Popped
0 ≤ N ≤ 100
10
FrozenSet from a List
Easy
Write a program to create a frozenset from a list and print elements sorted.
First line N. Second line N elements.
Print sorted unique elements.
Sample Input 1
7
3 1 4 1 5 9 2 6
Sample Output 1
1 2 3 4 5 6 9
Sample Input 2
4
d c b a
Sample Output 2
a b c d
Sample Input 3
5
10 10 10 10 10
Sample Output 3
10
Sample Input 4
5
0 1 -1 1 0
Sample Output 4
-1 0 1
1 ≤ N ≤ 100
1
Union of Two Sets
Medium
Write a program to find the union of two sets.
First line N1. Second line N1 elements. Third line N2. Fourth line N2 elements.
Print union sorted.
Sample Input 1
4
1 2 3 4
4
4 5 6 7
Sample Output 1
1 2 3 4 5 6 7
Sample Input 2
3
a b c
3
b c d
Sample Output 2
a b c d
Sample Input 3
2
1 2
2
2 3
Sample Output 3
1 2 3
Sample Input 4
4
10 20 30 40
2
30 50
Sample Output 4
10 20 30 40 50
1 ≤ N1, N2 ≤ 100
2
Intersection of Two Sets
Medium
Write a program to find the intersection (common elements) of two sets.
First line N1 elements. Third line N2 elements.
Print intersection sorted. If empty, print "(empty)".
Sample Input 1
5
1 2 3 4 5
4
4 5 6 7
Sample Output 1
4 5
Sample Input 2
3
a b c
3
x y z
Sample Output 2
(empty)
Sample Input 3
4
1 2 3 4
3
2 5 6
Sample Output 3
2
Sample Input 4
3
x y z
2
x y
Sample Output 4
x y
1 ≤ N1, N2 ≤ 100
3
Difference of Two Sets
Medium
Write a program to find the set difference (in first, not in second).
First line N1. Second line N1 elements. Third line N2. Fourth line N2 elements.
Print difference sorted. If empty, print "(empty)".
Sample Input 1
6
1 2 3 4 5 6
4
4 5 6 7
Sample Output 1
1 2 3
Sample Input 2
3
a b c
2
x y
Sample Output 2
a b c
Sample Input 3
4
1 2 3 4
4
1 2 3 4
Sample Output 3
(empty)
Sample Input 4
3
10 20 30
3
20 30 40
Sample Output 4
10
1 ≤ N1, N2 ≤ 100
4
Symmetric Difference of Two Sets
Medium
Write a program to find symmetric difference (in either set, not both).
First line N1. Second line N1 elements. Third line N2. Fourth line N2 elements.
Print symmetric difference sorted. If empty, print "(empty)".
Sample Input 1
5
1 2 3 4 5
4
3 4 6 7
Sample Output 1
1 2 6 7
Sample Input 2
3
a b c
3
b c d
Sample Output 2
a d
Sample Input 3
3
x x x
3
x y z
Sample Output 3
y z
Sample Input 4
4
10 20 30 40
4
20 30 50 60
Sample Output 4
10 40 50 60
1 ≤ N1, N2 ≤ 100
5
Check if Set is Subset
Medium
Write a program to check if set A is a subset of set B.
First line N1 (set A). Second line N1 elements. Third line N2 (set B). Fourth line N2 elements.
Print "Yes" or "No".
Sample Input 1
3
1 2 3
5
1 2 3 4 5
Sample Output 1
Yes
Sample Input 2
4
1 2 3 4
3
1 2 3
Sample Output 2
No
Sample Input 3
1
a
3
a b c
Sample Output 3
Yes
Sample Input 4
3
x y z
3
x y z
Sample Output 4
Yes
1 ≤ N1, N2 ≤ 100
6
Check if Set is Superset
Medium
Write a program to check if set A is a superset of set B.
First line N1 (A). Second line N1 elements. Third line N2 (B). Fourth line N2 elements.
Print "Yes" or "No".
Sample Input 1
5
1 2 3 4 5
3
1 2 3
Sample Output 1
Yes
Sample Input 2
3
1 2 3
4
1 2 3 4
Sample Output 2
No
Sample Input 3
4
a b c d
2
a d
Sample Output 3
Yes
Sample Input 4
3
x y z
3
x y w
Sample Output 4
No
1 ≤ N1, N2 ≤ 100
7
Disjoint Sets Check
Medium
Write a program to check if two sets have no common elements.
First line N1. Second line N1 elements. Third line N2. Fourth line N2 elements.
Print "Yes" if disjoint, "No" otherwise.
Sample Input 1
4
1 2 3 4
4
4 5 6 7
Sample Output 1
No
Sample Input 2
3
a b c
3
x y z
Sample Output 2
Yes
Sample Input 3
2
10 20
3
30 40 50
Sample Output 3
Yes
Sample Input 4
3
cat dog fish
2
dog fish
Sample Output 4
No
1 ≤ N1, N2 ≤ 100
8
Remove Duplicates Using Set
Medium
Write a program to remove duplicates from a list using a set, then convert back to a sorted list.
First line N. Second line N integers.
Print sorted unique list.
Sample Input 1
8
3 1 4 1 5 3 2 4
Sample Output 1
1 2 3 4 5
Sample Input 2
7
10 10 20 20 50 30 40
Sample Output 2
10 20 30 40 50
Sample Input 3
6
2 -3 5 -5 0 2
Sample Output 3
-5 -3 0 2 5
Sample Input 4
6
7 8 9 9 8 7
Sample Output 4
7 8 9
1 ≤ N ≤ 100
9
Count Unique Words in Sentence
Medium
Write a program to count unique words in a sentence (case-sensitive).
A single line containing a sentence.
Print the number of unique words.
Sample Input 1
the cat and the dog and the bird
Sample Output 1
5
Sample Input 2
hello world hello
Sample Output 2
2
Sample Input 3
Python python PYTHON
Sample Output 3
3
Sample Input 4
a b c d e f g
Sample Output 4
7
Length ≤ 500
10
Sum of Unique Elements
Medium
Write a program to find the sum of all unique elements in a list.
First line N. Second line N integers.
Print sum of unique elements.
Sample Input 1
10
1 2 3 4 5 1 2 3 4 5
Sample Output 1
15
Sample Input 2
6
1 1 2 2 3 3
Sample Output 2
6
Sample Input 3
5
10 10 10 10 10
Sample Output 3
10
Sample Input 4
9
7 3 5 3 7 5 7 3 5
Sample Output 4
15
1 ≤ N ≤ 100
1
Set Operations — Universal Set
Hard
Write a program to find the complement of a set A relative to a universal set U (1 to N).
First line contains N (size of U). Second line contains M elements of set A.
Print the complement sorted ascending. If empty, print "(empty)".
Sample Input 1
10
1 3 5 7 9
Sample Output 1
2 4 6 8 10
Sample Input 2
5
1 2 3 4 5
Sample Output 2
(empty)
Sample Input 3
7
1 3 5
Sample Output 3
2 4 6 7
Sample Input 4
3
1 2
Sample Output 4
3
1 ≤ N ≤ 100
2
Max and Min Element in a Set
Hard
Write a program to find the maximum and minimum element in a set.
First line contains N. Second line contains N space-separated 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
3
-10 -5 -20
Sample Output 2
Min: -20 Max: -5
Sample Input 3
1
42
Sample Output 3
Min: 42 Max: 42
Sample Input 4
5
100 0 -100 50 75
Sample Output 4
Min: -100 Max: 100
1 ≤ N ≤ 100
3
Update Set with List
Hard
Write a program to update a set with elements from a list (add all list elements to the set).
First line N (initial set elements). Second line N elements. Third line M (list elements to add). Fourth line M elements.
Print final set sorted ascending.
Sample Input 1
3
1 2 3
3
3 4 5
Sample Output 1
1 2 3 4 5
Sample Input 2
0

4
5 5 6 7
Sample Output 2
5 6 7
Sample Input 3
3
a b c
2
b d
Sample Output 3
a b c d
Sample Input 4
4
10 20 30 40
2
50 60
Sample Output 4
10 20 30 40 50 60
0 ≤ N, M ≤ 100
4
Unique Characters in String
Hard
Write a program to find the number of unique characters in a given string (case-sensitive).
A single line containing a string.
Print the count of unique characters.
Sample Input 1
hello world
Sample Output 1
8
Sample Input 2
aabbcc
Sample Output 2
3
Sample Input 3
Python
Sample Output 3
5
Sample Input 4
aaaaa
Sample Output 4
1
String length ≤ 500
5
Set of Divisors
Hard
Write a program to find all divisors of a given integer N as a set.
First line contains N (1 ≤ N ≤ 1000).
Print the set of divisors sorted ascending.
Sample Input 1
12
Sample Output 1
1 2 3 4 6 12
Sample Input 2
7
Sample Output 2
1 7
Sample Input 3
1
Sample Output 3
1
Sample Input 4
16
Sample Output 4
1 2 4 8 16
1 ≤ N ≤ 1000
6
Common Elements in Three Sets
Hard
Write a program to find elements common to all three sets (intersection of 3 sets).
First line N1 elements. Third line N2 elements. Fifth line N3 elements.
Print common elements sorted ascending. If empty, print "(empty)".
Sample Input 1
4
1 2 3 4
4
2 3 4 5
3
2 4 6
Sample Output 1
2 4
Sample Input 2
3
a b c
3
d e f
3
g h i
Sample Output 2
(empty)
Sample Input 3
3
1 2 3
3
1 2 4
3
1 2 5
Sample Output 3
1 2
Sample Input 4
4
x y z w
2
x z
3
x y z
Sample Output 4
x z
1 ≤ N1, N2, N3 ≤ 100
7
Set of Factors — Prime Factorization
Hard
Write a program to find the set of prime factors of a given integer N.
First line contains N (2 ≤ N ≤ 1000).
Print the set of prime factors sorted ascending.
Sample Input 1
12
Sample Output 1
2 3
Sample Input 2
30
Sample Output 2
2 3 5
Sample Input 3
7
Sample Output 3
7
Sample Input 4
100
Sample Output 4
2 5
2 ≤ N ≤ 1000
8
Check if Two Sets are Equal
Hard
Write a program to check if two sets are equal (contain exactly the same elements).
First line N1 elements. Third line N2 elements.
Print "Equal" or "Not equal".
Sample Input 1
4
1 2 3 4
4
4 3 2 1
Sample Output 1
Equal
Sample Input 2
3
a b c
3
a b d
Sample Output 2
Not equal
Sample Input 3
1
5
1
5
Sample Output 3
Equal
Sample Input 4
4
1 1 2 2
2
1 2
Sample Output 4
Equal
1 ≤ N1, N2 ≤ 100
9
Set of Perfect Squares up to N
Hard
Write a program to generate the set of all perfect squares less than or equal to N.
First line contains N (1 ≤ N ≤ 1000).
Print the set sorted ascending.
Sample Input 1
20
Sample Output 1
1 4 9 16
Sample Input 2
10
Sample Output 2
1 4 9
Sample Input 3
1
Sample Output 3
1
Sample Input 4
50
Sample Output 4
1 4 9 16 25 36 49
1 ≤ N ≤ 1000
10
Power Set Size
Hard
Write a program to calculate the number of subsets (power set size) of a given set of N elements.
First line contains integer N.
Print 2^N — the size of the power set.
Sample Input 1
3
Sample Output 1
8
Sample Input 2
0
Sample Output 2
1
Sample Input 3
5
Sample Output 3
32
Sample Input 4
10
Sample Output 4
1024
0 ≤ N ≤ 30