Set Theory
A set is simply a set of objects. They don't have to be the same type, or have any relation to one another.
Sets are denoted with parenthesis ( )
. With the separation of elements being a comma. Or sometimes { }
.
Sets follow 2 rules:
Only unique items.
Sets can only contain unique items. It cannot contain the same item twice or more.
The set is unordered.
There is no order or structure to a set.
The first rule is quite useful. Say, for example we have a list of words from a book:
To find all the unique words, we put the words into a set:
The 2nd rule ties in nicely with the 1st rule. A set aims to replicate how we see things in our lives.
Imagine we are on a road trip with Jahan, Olivia, and Ryan.
Next year, we want to go on a field trip again. It doesn't make much sense to invite Jahan, Olivia, Jahan, Ryan, Jahan.
And it also doesn't make sense to assign an "order" to them. They're people, not elements in an array!
There are other types of sets too. Such as a a multiset which allows non-unique elements but no order.
Note sets are a computer science / maths topic. I will quickly go over the maths, and we will explore how this is applied in Python,
š¦ Cardinality
The cardinality of a set is the length of the set. For the set:
The cardinality is
š“āāļø Equality of Sets
2 sets are equal when all the elements match.
Remember, order doesn't matter - so sets are equal despite being in the 'wrong' order!
š Infinite Sets
Some sets are infinite. Such as the set containing all the natural numbers, or all the real numbers.
We can express infinite sets using a little bit of maths. Heard of list comprehensions? It looks the same syntactically, but operates infinitely.
The set of all even numbers is:
Read this as "for each number, n, in the set of all integers, Z, multiply the number by 2". Which will give us the set of all even numbers.
In a list comprehension, it would look like:
However, this won't work as Python doesn't allow infinite sets.
šāā¬ The Empty Set
The empty set is the set containing nothing at all. It is much like 0. It is nothing, and serves the purpose as a negated value. We don't have 0 apples, we have no apples.
The empty set serves the same purpose. We don't have a set containing all the words, we have nothing.
Its symbol is ā .
š The Universal Set
The Universal Set U is the set which contains all objects, including itself.
If our universe contains only integers, 1, 2, 3, ..., then the universal set is the set of all integers.
š„ Operations
Just like with other data types, sets have operations! Let's start with some operations you may already know.
ā Union
The union is combining 2 sets together (think addition). The symbol for the union is āŖ. Let's look at an example.
Notice how when we perform a union, we have two 3's. Since sets only contain unique elements, we simply toss the extra 3 away. Also note, there is no order. So B āŖ
A does not result in (3, 4, 5...) as the order doesn't exist. We can even say B āŖ A = {3, 5, 1, 4, 2}
.What if we union an empty set with a non-empty set? Or an empty set with another empty set?
When we get onto subsets and the likes we'll learn to appreciate the empty set and this simplistic maths.
Last updated