Python Java SQL Course Notes C C++ HTML CSS JS

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
Competitive MCQs — Set
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 34
Q1
Which of these creates a set?
Correct!
Wrong — correct answer is .
Curly braces with values create a set (empty {} is a dict).
Q2
What is TRUE about Python sets?
Correct!
Wrong — correct answer is .
Sets are unordered collections of unique, hashable items.
Q3
What is the output of this code?
python
1
2
s = {1, 2, 2, 3}
print(len(s))
Correct!
Wrong — correct answer is .
Duplicates are removed, so the set holds {1, 2, 3}.
Q4
What is the output of this code?
s = {1, 2, 2, 3}
print(len(s))
Correct!
Wrong — correct answer is .
Duplicates are removed, so the set holds {1, 2, 3}.
Q5
Which method adds an element to a set?
Correct!
Wrong — correct answer is .
Sets use add(); append is for lists.
Q6
What is the output of this code?
python
1
2
3
s = {1, 2, 3}
s.remove(2)
print(s)
Correct!
Wrong — correct answer is .
remove() deletes the given element from the set.
Q7
What is the output of this code?
s = {1, 2, 3}
s.remove(2)
print(s)
Correct!
Wrong — correct answer is .
remove() deletes the given element from the set.
Q8
Which set operation returns elements common to two sets?
Correct!
Wrong — correct answer is .
intersection() returns elements present in both sets.
Q9
What is the output of this code?
python
1
print({1, 2, 3} | {3, 4})
Correct!
Wrong — correct answer is .
| is the union operator: combines all unique elements.
Q10
What is the output of this code?
print({1, 2, 3} | {3, 4})
Correct!
Wrong — correct answer is .
| is the union operator: combines all unique elements.
Q11
Which of the following CANNOT be stored in a set?
Correct!
Wrong — correct answer is .
Sets require hashable items; lists are unhashable (mutable).
Q12
What is the output of this code?
python
1
2
s = {5, 1, 3}
print(3 in s)
Correct!
Wrong — correct answer is .
in checks membership; 3 is present in the set.
Q13
What is the output of this code?
s = {5, 1, 3}
print(3 in s)
Correct!
Wrong — correct answer is .
in checks membership; 3 is present in the set.
Q14
What does the difference() method return?
Correct!
Wrong — correct answer is .
s1.difference(s2) (or s1 - s2) removes items found in s2.
Q15
What is the output of this code?
s = set([1, 2, 2, 3])
print(s)
Correct!
Wrong — correct answer is .
set() removes duplicates, leaving the unique values {1, 2, 3}.
Q16
Which set method removes a random element and returns it?
Correct!
Wrong — correct answer is .
Set pop() removes and returns an arbitrary element because sets are unordered.
Q17
What is the output of this code?
s = {1, 2, 3}
s.discard(9)
print(s)
Correct!
Wrong — correct answer is .
discard() silently does nothing when the element is absent, unlike remove().
Q18
What is the output of this code?
s = {1, 2}
s.update([3, 4])
print(s)
Correct!
Wrong — correct answer is .
update() adds every element from the iterable into the set.
Q19
Which operation returns elements in either set?
Correct!
Wrong — correct answer is .
s1.union(s2) (or s1 | s2) combines all unique elements from both sets.
Q20
What is the output of this code?
print({1, 2} & {2, 3})
Correct!
Wrong — correct answer is .
& performs intersection, returning elements common to both: {2}.
Q21
What is the output of this code?
print({1, 2, 3} - {2})
Correct!
Wrong — correct answer is .
The - operator computes the difference, removing 2 from the first set.
Q22
What is the output of this code?
print({1, 2} ^ {2, 3})
Correct!
Wrong — correct answer is .
^ is symmetric difference: elements in exactly one set, so {1, 3}.
Q23
What does {1, 2}.issubset({1, 2, 3}) return?
Correct!
Wrong — correct answer is .
issubset() checks whether every element of the first set is in the second.
Q24
What is the output of this code?
s = {1, 2, 3}
s.clear()
print(len(s))
Correct!
Wrong — correct answer is .
clear() empties the set, so its length becomes 0.
Q25
How do you create an empty set?
Correct!
Wrong — correct answer is .
{} makes an empty dict; the set constructor set() creates an empty set.
Q26
Can a set contain a tuple?
Correct!
Wrong — correct answer is .
Immutable tuples are hashable, so they may be stored in a set.
Q27
What is the output of this code?
s = {1, 2, 3}
print(len(s))
Correct!
Wrong — correct answer is .
len() counts the unique elements in the set, which is 3.
Q28
Which method returns True if two sets share no elements?
Correct!
Wrong — correct answer is .
isdisjoint() returns True when the two sets have no common elements.
Q29
What is the output of this code?
print({1, 2, 3}.isdisjoint({4, 5}))
Correct!
Wrong — correct answer is .
The two sets share no values, so isdisjoint() returns True.
Q30
Which of these raises TypeError when added to a set?
Correct!
Wrong — correct answer is .
Lists are mutable and therefore unhashable, so they cannot enter a set.
Q31
What is the output of this code?
s = {3, 1, 2}
print(2 in s)
Correct!
Wrong — correct answer is .
Membership with in works on sets just like other collections.
Q32
What is the output of this code?
s = {1, 2}
s.add(3)
s.add(1)
print(len(s))
Correct!
Wrong — correct answer is .
Adding 1 again changes nothing, so the set ends with {1, 2, 3}.
Q33
What is the output of this code?
s1 = {1, 2}
s2 = {1, 2}
print(s1 == s2)
Correct!
Wrong — correct answer is .
Set equality compares contents regardless of order, so the two sets are equal.
Q34
Which expression removes 5 from set s but does NOT raise an error if absent?
Correct!
Wrong — correct answer is .
discard() never raises; remove() raises KeyError for missing values.