Tip:
Highlight text to annotate it
X
The set is an important tool in Python, and today we're going to talk about it.
A set in Python is simply an unordered collection of elements with no duplicates.
This unordered and no duplication--these are important clauses.
The sets used by Python are just a programmatic version of the sets used by mathematicians.
Let's talk a bit about set theory.
Let's say we have a basket of fruit, and let's say it contains 5 apples, 3 oranges, and 2 tomatoes.
These I suppose would be the apples or maybe the tomatoes, but we don't have to worry about that.
If we want to represent this as a set, we wouldn't care about this quantity.
The fact that there is 5 apples doesn't matter.
In fact, I could just write apple, orange, and tomato
and label this set "fruit."
The elements of this set "fruit" are simply the entries apple, orange, and tomato.
Let's say we also have a set called "vegetable."
Some of the elements in this set would be broccoli, carrot, and tomato.
When I was coming up with this question there was some heated debate in the office
about whether tomatoes are fruits or vegetables. Let's just include it in both.
That means that when I draw the circle that encompasses this set,
it actually has an overlap with the set fruit.
In set theory, this overlap is called the intersection.
I'm going to mention these set theory terms, but you don't have to worry about them too much.
This diagram I've drawn here is known in set theory as a Venn diagram.
They can be very useful for visualizing what's going on with sets.
Here comes the interesting part.
We can create new sets by taking subsections or performing operations on these sets.
For example, if we want to know what elements are fruits and vegetables,
which like I said before is called the intersection,
we could write "Fruit & Vegetable," and we could see the only element would be tomato.
I should point out that this symbol and the other symbols I will be using are not
what we usually use in mathematical set theory,
but since they're what we use in Python I'm going to be sticking with this convention.
We could also ask what elements are fruits but not vegetables,
which is known as the set difference.
The only element that are fruits but not vegetables are apple and orange.
We could ask which elements are fruits or vegetables.
Here I've used the vertical line to indicate that. This would give us all of this entries.
All of these entries are either a fruit or a vegetable or both.
The last operation I'm going to talk about is asking
which elements are fruits or vegetables but not both.
This is known as the symmetric difference.
Here we've used the caret to indicate that, which is not the same as this carrot.
This is just all of the entries except for tomato.