-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.py
94 lines (54 loc) · 1.15 KB
/
set.py
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
#!/usr/bin/python
# set
set1 = {"Weimann", "Dickens", "Lakin"}
print(set1)
# output:
# {'Weimann', 'Lakin', 'Dickens'}
# check if "Weimann" is present in the set
set2 = {"Weimann", "Dickens", "Lakin"}
print("Weimann" in set2)
# output:
# True
# add
set3 = {"Weimann", "Dickens", "Lakin"}
set3.add("Schneider")
print(set3)
# output:
# {'Lakin', 'Dickens', 'Schneider', 'Weimann'}
# add multiple items to a set
set4 = {"Weimann", "Dickens", "Lakin"}
set4.update(["Schneider", "Kshlerin", "Pfannerstill"])
print(set4)
# output:
# {'Dickens', 'Kshlerin', 'Schneider', 'Weimann', 'Lakin', 'Pfannerstill'}
# get the length of a set
set5 = {"Weimann", "Dickens", "Lakin"}
print(len(set5))
# output:
# 3
# remove
set6 = {"Weimann", "Dickens", "Lakin"}
set6.remove("Dickens")
print(set6)
# output:
# {'Lakin', 'Weimann'}
# clear
set7 = {"Weimann", "Dickens", "Lakin"}
set7.clear()
print(set7)
# output
# set()
# del
set8 = {"Weimann", "Dickens", "Lakin"}
del set8
try:
print(set8)
except:
print("Not found")
# output:
# Not found
# set constructor
set9 = set(("Weimann", "Dickens", "Lakin"))
print(set9)
# output:
# {'Weimann', 'Dickens', 'Lakin'}