Practice Top 30 Python Programming Questions on Sets Data Structure
Q1. Write a program to create a set.
Input:
{10, 20, 30}
Expected Output:
Set: {10, 20, 30}
Q2. Write a program to add elements to a set.
Input:
Set: {1, 2, 3}
Element to add: 4
Expected Output:
Updated Set: {1, 2, 3, 4}
Q3. Write a program to remove an element from a set.
Input:
Set: {1, 2, 3, 4}
Element to remove: 3
Expected Output:
Updated Set: {1, 2, 4}
Q4. Write a program to check if an element exists in a set.
Input:
Set: {10, 20, 30, 40}
Element to check: 30
Expected Output:
Element 30 exists in the set.
Q5. Write a program to find the length of a set.
Input:
Set: {1, 2, 3, 4, 5}
Expected Output:
Length of set: 5
Q6. Write a program to find the union of two sets.
Input:
Set 1: {1, 2, 3}
Set 2: {3, 4, 5}
Expected Output:
Union: {1, 2, 3, 4, 5}
Q7. Write a program to find the intersection of two sets.
Input:
Set 1: {1, 2, 3}
Set 2: {3, 4, 5}
Expected Output:
Intersection: {3}
Q8. Write a program to find the difference between two sets.
Input:
Set 1: {1, 2, 3}
Set 2: {3, 4, 5}
Expected Output:
Difference: {1, 2}
Q9. Write a program to find the symmetric difference between two sets.
Input:
Set 1: {1, 2, 3}
Set 2: {3, 4, 5}
Expected Output:
Symmetric Difference: {1, 2, 4, 5}
Q10. Write a program to convert a list to a set.
Input:
List: [1, 2, 3, 3, 4]
Expected Output:
Set: {1, 2, 3, 4}
Q11. Write a program to clear all elements in a set.
Input:
Set: {1, 2, 3, 4}
Expected Output:
Set after clearing: set()
Q12. Write a program to create a copy of a set.
Input:
Set: {1, 2, 3}
Expected Output:
Copied Set: {1, 2, 3}
Q13. Write a program to create a set containing different data types.
Input:
Set: {1, 'apple', 3.14, True}
Expected Output:
Set with mixed data types: {1, 'apple', 3.14, True}
Q14. Write a program to create a set from a string (removing duplicate characters).
Input:
String: 'hello'
Expected Output:
Set from string: {'h', 'e', 'l', 'o'}
Q15. Write a program to create a set using set comprehension.
Input:
List: [1, 2, 3, 4, 5]
Condition: x 2 for x in list if x % 2 == 0
Expected Output:
Set using comprehension: {4, 8}
Q16. Write a program to check if one set is a subset of another.
Input:
Set 1: {1, 2}
Set 2: {1, 2, 3, 4}
Expected Output:
Set 1 is a subset of Set 2.
Q17. Write a program to check if one set is a superset of another.
Input:
Set 1: {1, 2, 3, 4}
Set 2: {1, 2}
Expected Output:
Set 1 is a superset of Set 2.
Q18. Write a program to create a set with a single element.
Input:
Element: 10
Expected Output:
Set with one element: {10}
Q19. Write a program to find common elements between two sets.
Input:
Set 1: {1, 2, 3}
Set 2: {3, 4, 5}
Expected Output:
Common elements: {3}
Q20. Write a program to create a set from multiple lists.
Input:
List 1: [1, 2, 3]
List 2: [3, 4, 5]
Expected Output:
Set from lists: {1, 2, 3, 4, 5}
Q21. Write a program to create a set that only contains unique elements from a list.
Input:
List: [1, 2, 2, 3, 4, 4, 5]
Expected Output:
Set with unique elements: {1, 2, 3, 4, 5}
Q22. Write a program to check if a set is empty.
Input:
Set: set()
Expected Output:
The set is empty.
Q23. Write a program to pop an element from a set.
Input:
Set: {1, 2, 3, 4}
Expected Output:
Popped element: 1
Updated Set: {2, 3, 4}
Q24. Write a program to find the union of multiple sets.
Input:
Set 1: {1, 2}
Set 2: {3, 4}
Set 3: {5, 6}
Expected Output:
Union of all sets: {1, 2, 3, 4, 5, 6}
Q25. Write a program to find the maximum element in a set.
Input:
Set: {10, 20, 30, 40}
Expected Output:
Maximum element: 40
Q26. Write a program to find the minimum element in a set.
Input:
Set: {10, 20, 30, 40}
Expected Output:
Minimum element: 10
Q27. Write a program to find the intersection of multiple sets.
Input:
Set 1: {1, 2, 3, 4}
Set 2: {3, 4, 5, 6}
Set 3: {4, 5, 6, 7}
Expected Output:
Intersection of all sets: {4}
Q28. Write a program to add elements from one set into another.
Input:
Set 1: {1, 2}
Set 2: {3, 4}
Expected Output:
Set 1 after adding elements: {1, 2, 3, 4}
Q29. Write a program to remove all elements from a set using the clear() method.
Input:
Set: {1, 2, 3, 4}
Expected Output:
Set after clear: set()
Q30. Write a program to update a set by removing elements found in another set.
Input:
Set 1: {1, 2, 3, 4, 5}
Set 2: {4, 5, 6, 7}
Expected Output:
Set 1 after difference update: {1, 2, 3}
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!