0% found this document useful (0 votes)
2 views

python set problems

The document provides a series of Python programming exercises focused on set operations, including creation, iteration, addition, removal, intersection, union, and conversion between data types. Each exercise includes sample outputs demonstrating the expected results of the operations. The exercises cover various aspects of set manipulation and usage in Python.

Uploaded by

anuanamika0220
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python set problems

The document provides a series of Python programming exercises focused on set operations, including creation, iteration, addition, removal, intersection, union, and conversion between data types. Each exercise includes sample outputs demonstrating the expected results of the operations. The exercises cover various aspects of set manipulation and usage in Python.

Uploaded by

anuanamika0220
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Write a Python program to create a set


Sample Output
{'T', True, 45, 23, 56, 'Joes', 45.6}
Type = < class 'set'>
2. Write a Python program to iteration over sets
Sample Output
{10,40,60,20,80,50}
40 10 80 50 20 60
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'}
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}
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}
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}
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}
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}
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}
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"}
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}
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
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
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()
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]
16. Write a Python program to Convert String to Set
Sample Output
"TutorJoes"
{'t', 'r', 'o', 'e', 's', 'u', 'J', 'T'}
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'}
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']
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')
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'}
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}
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}
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
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
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
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
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
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}
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
30. program to count number of vowels using sets in given string
Sample Output
String = "Tutor Joes"
Number of Vowels = 4

You might also like