Nelson
10/12/2020
From the Datacamp Tutorial on sets.
“The major difference is that sets, unlike lists or tuples, cannot have multiple occurrences of the same element and store unordered values.”
Create ASet, AList and ATuple, all of which contain “cat”, “dog” and “parrot”. Print the type of each.
AList = ["cat","dog","parrot"]
ATuple = ("cat","dog","parrot")
ASet = {"cat","dog","parrot"}
print(type(AList))
## <class 'list'>
## <class 'tuple'>
## <class 'set'>
Attempt to obtain the first value of each collection. How does python respond?
## cat
## cat
Write for loops to iterate over and print the contents of these collections. Observations?
## cat
## dog
## parrot
## cat
## dog
## parrot
## dog
## parrot
## cat
I observed that the order of items printed from the set was not what I used when I created the set.
Create BList, BTuple and BSet. They have the same items as the A variants, but the cat appears last instead of first.
Test the A and B variants for equality. What do you observe?
## False
## False
## True
With lists and tuples, order matters, but this is not true for sets.
To each of the collection definitions, add another “dog” as a fourth entry.
Print each of these and observe the difference.
## ['cat', 'dog', 'parrot', 'dog']
## ('cat', 'dog', 'parrot', 'dog')
## {'dog', 'parrot', 'cat'}
Observe that entering the second “dog” changed the list and the tuple, but not the set.
Try the len function on the C variants.
These two data structures share the curly. In the case of a non-empty curly a set can be distinguished from a dictionary by the absence of the : structure in the case of a set.
## {'dog': 2, 'cat': 1, 'parrot': 0}
## {'Fido', 'Rover', 'Felix'}
This implies that two curlies with nothing included must be an empty dictionary. An empty set is given by set().
Add a python to the dictionary and the set. The pythons name is “Cool guy”.
## {'dog': 2, 'cat': 1, 'parrot': 0, 'Python': 1}
## {'Fido', 'Rover', 'Felix', 'Cool guy'}
Note that sets are quite mutable.
The cat died, so we want to update the set and dictionary.
## {'dog': 2, 'cat': 0, 'parrot': 0, 'Python': 1}
## {'Fido', 'Rover', 'Cool guy'}
Note that the set doesn’t print in alphabetical order. Use the sorted function to “solve” this problem and create sorted_pets. What is the type of this new object?
## <class 'list'>
## ['Cool guy', 'Fido', 'Rover']
The function set() turns a list into a set. A set is unduplicated. the function list() turns a set into a list.
Use these facts to write a function undup(), which converts a list with duplications into an unduplicated list. Test it on [1,1,2,3,4,4,5,1,2,6]
## [1, 2, 3, 4, 5, 6]
Does the set() function work on range objects?
## {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}
## 100
## <class 'set'>
Python somewhat awkwardly displays the result as a “set of a list”.
Let’s use this set as our universe to discuss some operations on its subsets. How many subsets does it have?
\[ 2^{100} \]
Start with Evens, the subset of even numbers.
## {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100}
There is an alternative to this standard code snippet. We can create the subset using a comprehension. I’ll create the set Evens_comp and see if it is identical to Evens.
## True
This notation “x for x in” seems redundant, but it has a purpose. We could compute the squares of the even numbers as follows.
## {256, 1024, 2304, 4096, 4, 900, 1156, 3844, 4356, 6400, 8836, 9604, 10000, 16, 144, 400, 784, 1296, 1936, 2704, 3600, 4624, 5776, 9216, 36, 676, 1444, 3364, 4900, 8100, 7056, 7744, 64, 576, 1600, 3136, 196, 324, 2116, 2500, 5184, 6084, 6724, 8464, 100, 484, 1764, 2916, 5476, 7396}
Create the set of odds using comprehension.
## {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99}
Note that the symbol we use to build the comprehension does not appear in the result. We just have to use it consistently when we define the comprehension. Any valid name would produce the same result.
Create the set HalfOne consisting of every number 50 or less. Hint: Put a conditional at the end of the comprehension definition.
## {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}
Create the sets Div3 , Div4, and Div5 containing the numbers divisible by 3, 4, and 5 respectively.
Create and print the intersection of Div3 and Div5.
Create and print the union of Div3 and Div5.
## {3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 87, 90, 93, 95, 96, 99, 100}
Create and print the set difference of Div3 and Div5.
## {3, 6, 9, 12, 18, 21, 24, 27, 33, 36, 39, 42, 48, 51, 54, 57, 63, 66, 69, 72, 78, 81, 84, 87, 93, 96, 99}
Create and print the symmetric difference of Div3 and Div5.
## {3, 5, 6, 9, 10, 12, 18, 20, 21, 24, 25, 27, 33, 35, 36, 39, 40, 42, 48, 50, 51, 54, 55, 57, 63, 65, 66, 69, 70, 72, 78, 80, 81, 84, 85, 87, 93, 95, 96, 99, 100}
There is no built-in complement function or operator in python since there is no default universe. However, once you have defined a universe, you can use the difference operator with this set to define a complement function.
Create a function comp() for our example. Test it by seeing if the complement of Evens is Odds.
In general, the theory of boolean algebra says that for any sets, A, B, and C, the following is true.
\[ A\cup(B\cap C) = (A\cup B)\cap(A\cup C) \]
Show that this is true for an example using Div3, Div4 and Div5