-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_set.py
More file actions
149 lines (100 loc) · 4.44 KB
/
4_set.py
File metadata and controls
149 lines (100 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 📌 1. Creating a Set
fruits = {"🍎", "🍌", "🍊", "🍇"}
print("🌟 Initial Set:", fruits)
# 📌 2. Adding Elements
print("\n➕ Adding Elements:")
fruits.add("🍉") # Add a single element
print("✅ After Adding 🍉:", fruits)
fruits.update(["🍒", "🍍"]) # Add multiple elements
print("✅ After Adding Multiple Fruits:", fruits)
# 📌 3. Removing Elements
print("\n❌ Removing Elements:")
fruits.remove("🍌") # Remove an element (raises KeyError if not found)
print("✅ After Removing 🍌:", fruits)
fruits.discard("🍍") # Remove an element (does not raise an error if not found)
print("✅ After Discarding 🍍:", fruits)
popped_fruit = fruits.pop() # Remove and return an arbitrary element
print("✅ Popped Fruit:", popped_fruit)
print("✅ After Popping:", fruits)
# 📌 4. Checking Membership
print("\n🧐 Checking Membership:")
print("Is 🍎 in the set?", "🍎" in fruits) # True
print("Is 🍉 in the set?", "🍉" in fruits) # False
# 📌 5. Iterating Over a Set
print("\n🚶♂️ Iterating Over the Set:")
for fruit in fruits:
print(fruit, end=", ")
# 📌 6. Set Operations (Union, Intersection, Difference, Symmetric Difference)
set1 = {"🍎", "🍌", "🍊"}
set2 = {"🍊", "🍇", "🍉"}
print("\n\n🔗 Set Operations:")
union_set = set1.union(set2) # All unique elements from both sets
print("Union:", union_set)
intersection_set = set1.intersection(set2) # Common elements in both sets
print("Intersection:", intersection_set)
difference_set = set1.difference(set2) # Elements in set1 but not in set2
print("Difference (set1 - set2):", difference_set)
symmetric_difference_set = set1.symmetric_difference(set2) # Elements in either set but not both
print("Symmetric Difference:", symmetric_difference_set)
# 📌 7. Checking Subset and Superset
subset = {"🍊", "🍇"}
superset = {"🍎", "🍌", "🍊", "🍇", "🍉"}
print("\n🔍 Checking Subset and Superset:")
print("Is subset a subset of superset?", subset.issubset(superset)) # True
print("Is superset a superset of subset?", superset.issuperset(subset)) # True
# 📌 8. Clearing a Set
fruits.clear()
print("\n🧹 After Clearing the Set:", fruits)
# 📌 9. Frozen Sets (Immutable Sets)
frozen_fruits = frozenset({"🍎", "🍌", "🍊"})
print("\n🧊 Frozen Set:", frozen_fruits)
# Attempting to modify a frozen set (will raise an error)
try:
frozen_fruits.add("🍉") # ❌ AttributeError: 'frozenset' object has no attribute 'add'
except AttributeError as e:
print("❌ Error:", e)
# 📌 10. Set Comprehension
squares = {x**2 for x in range(1, 6)}
print("\n🔢 Set Comprehension (Squares):", squares)
# 📌 11. Converting Between Lists and Sets
fruit_list = ["🍎", "🍌", "🍊", "🍎", "🍌"]
unique_fruits = set(fruit_list) # Convert list to set to remove duplicates
print("\n🔄 List to Set:", unique_fruits)
back_to_list = list(unique_fruits) # Convert set back to list
print("🔄 Set to List:", back_to_list)
# 📌 12. Length of a Set
print("\n📏 Length of the Set:", len(unique_fruits))
# 📌 13. Copying a Set
copied_set = unique_fruits.copy()
print("\n📋 Copied Set:", copied_set)
#--------------------Expected Output-----------------------
🌟 Initial Set: {'🍊', '🍇', '🍎', '🍌'}
➕ Adding Elements:
✅ After Adding 🍉: {'🍊', '🍇', '🍎', '🍉', '🍌'}
✅ After Adding Multiple Fruits: {'🍊', '🍇', '🍎', '🍉', '🍒', '🍍', '🍌'}
❌ Removing Elements:
✅ After Removing 🍌: {'🍊', '🍇', '🍎', '🍉', '🍒', '🍍'}
✅ After Discarding 🍍: {'🍊', '🍇', '🍎', '🍉', '🍒'}
✅ Popped Fruit: 🍊
✅ After Popping: {'🍇', '🍎', '🍉', '🍒'}
🧐 Checking Membership:
Is 🍎 in the set? True
Is 🍉 in the set? True
🚶♂️ Iterating Over the Set:
🍇, 🍎, 🍉, 🍒,
🔗 Set Operations:
Union: {'🍇', '🍎', '🍉', '🍒', '🍊'}
Intersection: {'🍊'}
Difference (set1 - set2): {'🍎', '🍌'}
Symmetric Difference: {'🍎', '🍉', '🍇', '🍌'}
🔍 Checking Subset and Superset:
Is subset a subset of superset? True
Is superset a superset of subset? True
🧹 After Clearing the Set: set()
🧊 Frozen Set: frozenset({'🍊', '🍇', '🍎'})
❌ Error: 'frozenset' object has no attribute 'add'
🔢 Set Comprehension (Squares): {1, 4, 9, 16, 25}
🔄 List to Set: {'🍊', '🍇', '🍎', '🍌'}
🔄 Set to List: ['🍊', '🍇', '🍎', '🍌']
📏 Length of the Set: 4
📋 Copied Set: {'🍊', '🍇', '🍎', '🍌'}