-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path043.Sets.py
84 lines (73 loc) · 1.75 KB
/
043.Sets.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
farm_animals = {"sheep","cow","hen"}
print(farm_animals)
for animal in farm_animals:
print(animal)
print("*"*40)
wild_animals = set(["lion","tiger","panther","elephant","hare"])
print(wild_animals)
for animal in wild_animals:
print(animal)
farm_animals.add("horse")
wild_animals.add("horse")
print(farm_animals)
print(wild_animals)
#sets are unordered
print()
empty_set = set()
print(type(empty_set))
even = set(range(0,40,2))
print(even)
square_tuples = (4,6,9,16,25)
squares = set(square_tuples)
print(sorted(squares))
# print(len(squares))
# print()
# #union operation
# print(even.union(squares))
# print(len(even.union(squares)))
# print(squares.union(even))
#
# print("*"*40)
# print(even.intersection(squares))
# print(even & squares)
# print(squares.intersection(even))
# print(squares & even)
#
# #difference or subrtaction operation
# print("*"*40)
# print("even - squares:")
# print(sorted(even.difference(squares)))
# print(sorted(even-squares))
# print()
# print(sorted(even))
# print(squares)
# even.difference_update(squares)
# print(sorted(even))
# print("*"*40)
# print("symmetric even - squares:")
# print(sorted(even.symmetric_difference(squares)))
# print("symmetric squares - even:")
# print(sorted(squares.symmetric_difference(even)))
# print("*"*40)
# squares.discard(4)
# squares.remove(16)
# squares.discard(8) # no error
# print(squares)
#
# try:
# squares.remove(8) # gives error if doesn't exsist
# except KeyError:
# print("The item 8 is not a member of set")
# print("*"*40)
# a = set(range(0,10))
# b = set(range(0,5))
# if b.issubset(a):
# print("b is subset of a")
#
# if a.issuperset(b):
# print("a is superset of b")
#
# forzen sets are immutable sets
even = frozenset(range(0,100,2))
print(even)
#even.add(3) gives error