-
Notifications
You must be signed in to change notification settings - Fork 0
/
sets.py
28 lines (24 loc) · 1020 Bytes
/
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
# Creating a Set
this_set = {"apple", "banana", "cherry"}
print("A simple set")
print(this_set) # Returns {'apple', 'banana', 'cherry'}
print("-------------------------------------------------")
# Using the set() constructor to make a set
this_set = set(("apple", "banana", "cherry")) # note the double round-brackets
print("Using the set() constructor to make a set")
print(this_set) # Returns {'apple', 'banana', 'cherry'}
print("-------------------------------------------------")
# Adding an item
this_set.add("damson")
print("After adding an item")
print(this_set) # Returns {'cherry', 'damson', 'apple', 'banana'}
print("-------------------------------------------------")
# Removing an item
this_set.remove("banana")
print("After removing an item")
print(this_set) # Returns {'cherry', 'damson', 'apple'}
print("-------------------------------------------------")
# Length of the set
print("Length of the set")
print(len(this_set)) # Returns 3
print("-------------------------------------------------")