Sets Flashcards
What are sets? What are their properties?
A set is an unordered collection of distinct (no repeats) items that does not record the position or order of insertion. Sets use curly brackets {exp1, exp2… }
Set’s don’t support indexing, slicing or other sequence like behaviour
(because it doesn’t record position or order) but still mutable
Their purpose is to hold distinct items (no duplicates) Sets are iterable.
What is membership and how is it used?
The items in a set are called members. you can test for members by using the in operator:
cars: {“Toyota”, “Ford”…}
“Ford” in cars
True
What is Union and how is it used?
The union of two or more sets is a set that consists of all items in those sets. If the same item appears in more than one set, it will only appear in union once.
set_name.union(second_set, third_set…)
OR
set_name I second_set, thirds_set…
What is intersection and how is it used?
Intersection is used to get all the common items in sets. These items will appear once:
set_name.intersection(set_2, set_3…)
OR
set_name & (set_2, set_3…)
How do you create an empty set?
variable = set( )