Sets Flashcards

1
Q

What are sets? What are their properties?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is membership and how is it used?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Union and how is it used?

A

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…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is intersection and how is it used?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you create an empty set?

A

variable = set( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly