Python : Set - Exercises and Solution


1. Write a Python program to create a set

Sample Output

{'T', True, 45, 23, 56, 'Joes', 45.6}

Type = < class 'set'>

View Solution

2. Write a Python program to iteration over sets

Sample Output

{10,40,60,20,80,50}

40 10 80 50 20 60

View Solution

3. Write a Python program to add member(s) in a set

Sample Output

{"Lion", "Cat"}

Add single element = Dog

{'Cat', 'Lion', 'Dog'}

Add multiple items = ["Ponda", "Tiger"]

Set = {'Cat', 'Ponda', 'Lion', 'Dog', 'Tiger'}

View Solution

4. Write a Python program to remove item(s) from a given set

Sample Output

Before removing = {50, 20, 70, 60, 30}

After removing = {20, 70, 60, 30}

View Solution

5. Write a Python program to remove an item from a set if it is present in the set

Sample Output

{50, 20, 70, 60, 30}

Remove 50 from the said set = {70, 60, 30, 20}

View Solution

6. Write a Python program to create an intersection of sets

Sample Output

{40, 20, 70, 30}

{40, 50, 20, 60}

Intersection of two Sets = {40, 20}

View Solution

7. Write a Python program to create a union of sets

Sample Output

{40, 20, 70, 30}

{40, 50, 20, 60}

Union of two Sets = {70, 40, 50, 20, 60, 30}

View Solution

8. Write a Python program to create set difference

Sample Output

A = {80, 50, 20, 70, 40, 30}

B = {50, 20, 90, 40, 10, 60}

Difference of a - b = {80, 70, 30}

Difference of b - a = {90, 10, 60}

View Solution

9. Write a Python program to create a symmetric difference

Sample Output

A = {80, 50, 20, 70, 40, 30}

B = {50, 20, 90, 40, 10, 60}

a - b = {70, 10, 80, 90, 60, 30}

b - a = {70, 10, 80, 90, 60, 30}

View Solution

10. Write a Python program to create a shallow copy of sets

Sample Output

{"Tutor" , "Joes"}

{"Joes" , "Tutor"}

Shallow copy of set b : {"Joes" , "Tutor"}

View Solution

11. Write a Python program to find the elements in a given set that are not in another set

Sample Output

X = {50, 20, 70, 40, 10, 60, 30}

Y = {80, 50, 100, 70, 90, 60}

X and Y = {40, 10, 20, 30}

Y and X = {80, 90, 100}

View Solution

12. Write a Python program to check if two given sets have no elements in common

Sample Output

a = {23, 8, 56, 45, 78}

b = {42, 26, 55, 87}

Z = {46, 87}

Compare A and B : True

Compare B and Z : False

Compare A and Z : True

View Solution

13. Write a Python program to find maximum and the minimum value in a set

Sample Output

{17, 56, 23, 8, 10, 45}

Maximum : 56

Minimum : 8

View Solution

14. Write a Python program to remove all elements from a given set

Sample Output

{ "Red", "Green", "Pink", "White", "Black", "Yellow", "Blue" }

After Remove all Elements give Sets : set()

View Solution

15. Write a Python program to Intersection of two lists

Sample Output

[1,2,3,4,5,6,7,8]

[11,2,43,48,55,6,76,8]

Intersection of Two Lists : [8, 2, 6]

View Solution

16. Write a Python program to Convert String to Set

Sample Output

"TutorJoes"

{'t', 'r', 'o', 'e', 's', 'u', 'J', 'T'}

View Solution

17. Write a Python program to Convert Set to String

Sample Output

Set = {'T', 'u', 't', 'o', 'r', 'J', 'o', 'e' , 's'}

String = {'T', 's', 't', 'o', 'u', 'J', 'e', 'r'}

View Solution

18. Write a Python program to Convert Set to List

Sample Output

Set = {'T', 'u', 't', 'o', 'r', 'J', 'o', 'e' , 's'}

List = ['T', 's', 't', 'o', 'u', 'J', 'e', 'r']

View Solution

19. Write a Python program to Convert Set to Tuple

Sample Output

Set = {'T', 'u', 't', 'o', 'r', 'J', 'o', 'e' , 's'}

Tuple = ('T', 's', 't', 'o', 'u', 'J', 'e', 'r')

View Solution

20. Write a Python program to Convert Tuple to Set

Sample Output

Tuple = ('T', 'u', 't', 'o', 'r', 'J', 'o', 'e' , 's')

Set = {'J', 'o', 'u', 's', 'e', 'r', 'T', 't'}

View Solution

21. Write a program to add all its elements into a given set

Sample Output

{10,20,30,40,50}

[60,70,80,90,100]

{70, 10, 80, 20, 90, 30, 100, 40, 50, 60}

View Solution

22. Write a Python program to return a new set with unique items from both sets by removing duplicates.

Sample Output

{10, 20, 30, 40, 50}

{40, 50, 60, 70,80}

{70, 40, 10, 80, 50, 20, 60, 30}

View Solution

23. Write a Python program to Check if two sets have any elements in common. If yes, display the common elements

Sample Output

Set 1 = {1, 2, 3, 4, 5}

Set 2 = {5, 3, 7, 8, 9}

Set 3 = {6, 7, 8, 9, 10}

Sets 1 and 2 items in Common = {3, 5}

Sets 1 and 3 No items in Common

View Solution

24. Write a Python program to Check if a set a subset of another set

Sample Output

A = {4,5}

B = {1,2,3,4,5}

A Subset of B = True

B Subset of A = False

View Solution

25. Write a Python program to Check if a set is a subset, using comparison operators

Sample Output

A = {'a','b'}

B = {'a','b','c'}

A Subset of B = True

B Subset of A = False

View Solution

26. Write a Python program to Check is a set a subset of itself?

Sample Output

A = {1,2,3,4,5}

A Subset of Itself(A) : True

View Solution

27. Write a Python program to Check if a specific value exists in a set

Sample Output

Set = {10,20,30,40,50}

30 in Set = True

60 in Set = False

View Solution

28. Find the union, symmetric difference, and intersection of the two sets. Print the results of each operation

Sample Output

Set 1 = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

Set 2 = {20, ('Python', 'C') , 10, 11, ('J', 'O', 'E') }

Union = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, ('Python', 'C') , 20, ('J', 'O', 'E') }

Symmetric Difference = {5, 6, 7, 8, 9, ('Python', 'C') , 12, 13, 14, 20, ('J', 'O', 'E') }

Intersection = {10, 11}

View Solution

29. Create a sequence of numbers using range, then ask the user to enter a number. Inform the user whether or not their number was within the range you specified

Sample Output

Enter the Number : 10

The Number is a Within a Range

View Solution

30. program to count number of vowels using sets in given string

Sample Output

String = "Tutor Joes"

Number of Vowels = 4

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial