-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path19_Sets.py
More file actions
73 lines (54 loc) · 1.26 KB
/
19_Sets.py
File metadata and controls
73 lines (54 loc) · 1.26 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
# initializing empty set
s1 = set()
set1 = {1, 2, 3, 4}
print(set1)
# Using the set() function
set2 = set("GeeksForGeeks")
print(set2)
# Creating a Set with the use of a List
set3 = set(["Geeks", "For", "Geeks"])
print(set3)
# Creating a Set with the use of a tuple
tup = ("Geeks", "for", "Geeks")
print(set(tup))
# Creating a Set with the use of a dictionary
d = {"Geeks": 1, "for": 2, "Geeks": 3}
print(set(d))
# Unordered, Unindexed and Mutability
sets = {1, 2, 3, 4, 5}
print(sets)
try:
print(sets[1])
except TypeError as e:
print('Error', e)
# Adding Elements to a Set in Python
set4 = {3,4,5,6,7,8,9}
set4.add(10)
set4.update([11,"abcd"])
print(set4)
# Accessing a Set in Python
set5= {1, 2, 3, 4, 5}
for i in set5:
print(i,end=" ")
# Removing Elements from a Set
set6 = {1, 2, 3, 4, 5}
set6.remove(3)
print(set6)
# Discarding Elements from a Set
set7 = {1, 2, 3, 4, 5}
set7.discard(3)
print(set7)
# Popping Elements from a Set
set8 = {1, 2, 3, 4, 5}
popped_element = set8.pop()
print("Popped Element:", popped_element)
print("Set after popping an element:", set8)
# Clearing a Set
set9 = {1, 2, 3, 4, 5}
set9.clear()
print("Set after clearing:", set9)
# Frozen Sets
fset = frozenset([1,2,3,4,5,6,8])
print(fset)
s = {2,3,4,5,6}
print(frozenset(s))