Set Theory in Python

by Kardi Teknomo

As I explained in my previous Learning Python tutorial in people.Revoledu.com, Python has build-in Set as primitive data type along with list, tuple and dictionary. Thus, set operations like union (|), intersection (&), set difference (-) and symmetric difference (^) are natural in Python.

In this note, let us explore Set Theory in Python. Thus, we start with Set Theory as part of Discrete Mathematics and we want to explore how to represent it in Python.

Table of Contents

Preparation

To use Venn diagram in Python, you might need to install matplotlib-venn. The source code and documentation of this module is in GitHub.

In Command prompt or Anaconda prompt, type:

pip install matplotlib-venn

If you are using Google Colab, add an exclamation ! before pip

!pip install matplotlib-venn

After the installation, now you can import the libraries

Here is my functions to draw Venn diagrams with listing the few elements inside the diagram.

What is Set?

A set is a well-defined unordered collection with distinct objects. The objects are also called elements or items. The elements of a set are no duplicate objects.

Mathematically, we can describe a set either:

For example, $A=\left \{ 1,4,9,16 \right \} = \left \{ x^2 | x \in \mathbb{N}, x^2 \leq 20 \right \}$ where the universal set $\mathbb{U}$ is now a set of natural number $\mathbb{N}$ and the property $\textit{P}(x)$ is defined as $x^2 \leq 20 $.

In Python, a set is created by listing the elements in a pair of curly bracket.

In the example below, we define set A by listing the elements but it contains repetitive elements. Python automatically removes the duplicate elements and make them all distinct objects.

In the example below, we would like to show that a set is unordered collection. Order in a set does not matter. Python automatically reorder the elements of the set.

Universal Set

The universal set $\mathbb{U}$ is the set containing all objects or elements and of which all other sets are subsets. The Universal Set is the equivalent to logical value TRUE.

For example, $A=\left \{ 1,2,3 \right \} = \left \{ x | x \in \mathbb{Z}, 1 \leq x \leq 3 \right \}$ where the universal set $\mathbb{U}$ is now a set of integer $\mathbb{Z}$ and the property $\textit{P}(x)=1 \leq x \leq 3 $.

Notice that when we change the universal set $\mathbb{U}$ into the set of real number $\mathbb{R}$, such that $A=\left \{ x | x \in \mathbb{R}, 1 \leq x \leq 3 \right \}$, then the set has infinite members, it is no longer finite set.

If the sets are known, we can define the universal set simply by taking the union of all known sets such that all the known sets become its subset.

Now we can test our definition of universal set

Empty Set

The empty set or null set, denoted as $\varnothing$ or $\left \{ \right \}$, is a set that contain no element. The empty set is the equivalent to logical value FALSE.

In Python, when we define a set, it is an empty set

We can also define an empty set in Python as the following example.

The cardinality of an empty set is zero: $\left | \varnothing \right |=0$

Note that a set containing zero is not an empty set $\left \{ 0 \right \} \neq \varnothing $ because it has one element.

Set Elements

We usually use capital letters such as $A, B, ..., X, Y, Z$ to represent set variables and lowercase letters such as $a, b,..., x, y, z$ to represent elements. Mathematically, we write $x$ is an element of set $A$ as $x \in A$.

How do we represent "is an element of set" in Python?

Use in operator.

If $x$ is not an element of set $A$, we write mathematically as $a \notin A$.

How do we represent "is not an element of set" in Python?

Use not in operator.

Cardinality of Sets

Let $A$ be a finite set with $n$ distinct elements, where $n \geq 0$. Then $\left | A \right | =n$ , where the cardinality (number of elements) of $A$ is $n$.

A set with only one element is called a singleton.

In Python, we can use len() function to get the nmber of elements of a set.

While we cannot have a set of sets in Python, we can have a set of tuples.

Subset

If $A$ and $B$ are sets from a universe $\mathbb{U}$, then $A$ is a subset of $B$, denoted as $A \subseteq B$, happens if and only if every element of $A$ is also an element of $B$. We can formalize this definition as

$A \subseteq B \Leftrightarrow \forall x \left ( x \in A \rightarrow x \in B \right )$

Cardinality for finite set: $ A \subseteq B \rightarrow \left | A \right | \leqslant \left | B \right |$

How do we represent "is A a subset of B" in Python? Use set method issubset()

A.issubset(B)

Alternatively, we can also use <= operator

A < = B

Not Subset

Given the universal set $\mathbb{U}$, let $A,B\subseteq \mathbb{U}$, we wrote subset as $A \subseteq B \Leftrightarrow \forall x \left ( x \in A \rightarrow x \in B \right )$

Then, we can define not subset as $A \nsubseteq B \Leftrightarrow \neg \forall x \left ( x \in A \rightarrow x \in B \right )$

From predicate calculus, we know that $\neg \forall x \equiv \exists x \neg$.

Then, we can write not-subset as $A \nsubseteq B \Leftrightarrow \exists x \neg \left ( x \in A \rightarrow x \in B \right )$

From propositional calculus, we know that $ P \rightarrow Q \equiv \neg P \vee Q$

Then, we can write not-subset as $A \nsubseteq B \Leftrightarrow \exists x \neg \left (\neg x \in A \vee x \in B \right )$

From propositional calculus, de Morgan law, we know that $\neg\left ( P \vee Q \right )\equiv \left ( \neg P \wedge \neg Q \right )$

Then, we can write not-subset as $A \nsubseteq B \Leftrightarrow \exists x \left ( x \in A \wedge \neg\left ( x \in B \right ) \right )$

Thus, we have simpler definition of not-subset is $A \nsubseteq B \Leftrightarrow \exists x \left ( x \in A \wedge x \notin B \right )$

Proper Subset

A proper subset is the same as a subset, except that the sets can't be identical.

Suppose A and B are sets. A is a proper subset of B if A is subset of B and there exists at least one element in B that is not in A.

A is a proper subset of B is written matematically as $A \subset B$, defined as

$A \subset B\Leftrightarrow \forall x\left ( x \in A \rightarrow x \in B \right ) \wedge \exists x\left ( x \in B \cap x\notin A \right )$

From predicate calculus, we know that $\exists x \equiv \neg \forall x $, then we can write proper subset equivalently as

$A \subset B\Leftrightarrow \forall x\left ( x \in A \rightarrow x \in B \right ) \wedge \neg \forall x\left ( x \in B \rightarrow x\in A \right )$

Cardinality for finite set: $A \subset B \rightarrow \left | A \right | < \left | B \right |$

Python issubset method contains the equality $A \subseteq B$, it is not proper subset $A \subset B$.

How do we represent "is A a proper subset of B" in Python?

use < operator

A < B returns True if A is a proper subset of B

Transitive Subsets

Subset operation is transitive. If A is a subset of B and B is a subset of C then A is also a subset of C.

$\left( A \subseteq B \right) \cap \left( B \subseteq C \right) \rightarrow \left( A \subseteq C \right)$

The example and the Venn diagram is show below. The numbers inside the Venn diagram are the number of elements.

Superset

Suppose $A$ and $B$ are sets from a universe $\mathbb{U}$. If $A$ is a subset of $B$, then $B$ is the superset of $A$.

$A \subseteq B \rightarrow B \supseteq A$

How do we represent "is B a superset of A" in Python? Use issuperset() set method

B.issuperset(A)

Alternatively, we can use >= operator

B >= A

or > operator for proper superset

B > A

Equal Sets

For a given universal set $\mathbb{U}$, two sets are said to be equal if and only if they contain exactly the same elements, regardless the order.

In Python, we can test equality of two sets simply by two equal signs == like the following example.

In connection with subset, we say that set A is equal to set B if and only if A is a subset of B and B is also a subset of A. Two sets A and B are said to be equal if every element of A is an element of B and every element of B is an element of A.

$A=B \Leftrightarrow \left ( A \subseteq B \right ) \wedge \left ( B \subseteq A \right )$

Power Set

If $A$ is a set from the universal set $\mathbb{U}$, the power set $\boldsymbol{\texttt{P}}(A)$ of a set $A$ is the set of all subsets of $A$. If $A$ has $n$ elements in it then $\boldsymbol{\texttt{P}}(A)$ will have $2^n$ elements. Thus, the power set of $A$ is ofen written as $2^A$.

$\boldsymbol{\texttt{P}}(A) = 2^A = \left \{ B | B \subseteq A \right \}$

An empty set is always a subset of set $A$.

To get the power set in Python, we need to list all combinations of the elements of the input set. However, a set of sets is not allowed in Python. Thus, several methods had been proposed to compute power set and they produce the same results. Below are some of them.

Relationship of Power set to Combination

In general, any finite non empty set $A$ with $n$ elements would have $2^n$ subsets. That described as he cardinality of power set $\left | \boldsymbol{\texttt{P}}\left ( x \right ) \right | = 2^n$.

For any $0 \leqslant k \leqslant n$, there are $\binom{n}{k}$ subsets of size $k$.

Counting the subsets of $A$ according to the number, $k$, of elements in a subset, we have combinatorial identity for $n\geq 0$:

$\binom{n}{0} + \binom{n}{1} + \binom{n}{2} + ...+ \binom{n}{n} = \sum_{k=0}^{n} \binom{n}{k} = 2^n$

Cartesian Product

The cartesian product of two sets will be a set of all possible ordered pairs with the first element of each ordered pair from the first set and the second element from the second set.

The Cartesian product of two sets A and B is defined as the set:

$A \times B = \left \{ \left ( a,b \right ) | a \in A \cap b \in B \right \}$

Some properties of Cartesian Products:

Operations on Sets

Let us discuss about the following set operations:

Union

Set union of A and B return the set of all elements in either A or B or both. This is the equivalent to logical operation OR.

$A \cup B = \left ( x \in A \vee x \in B\right )$

Cardinality: $ \left | A \cup B \right | = \left | A \right | + \left | A \right | - \left | A \cap B \right |$

In Python, we can use either union() method or bar | operator.

A.union(B)

A|B

Intersection

Set intersection return the set of elements common to both A and B. This is the equivalent to logical operation AND.

$A \cap B = \left ( x \in A \wedge x \in B\right )$

In Python, we can use either intersection() method or ampersand & operator.

A.intersection(B)

A & B

Disjoint

Two sets are called disjoint if their intersection is empty, that is, they share no common elements.

$A \cap B = \varnothing$

In Python, we can test whether the two sets are disjoint using isdisjoint() method.

A.isdisjoint(B)

Set Difference

The difference between two sets A and B contains exactly those elements of A that are not in B:

$A - B = \left \{ x | x \in A \wedge x \notin B\right \}$

Cardinality: $ \left | A - B \right | = \left | A \right | - \left | A \cap B \right |$

In Python, we can use either difference() method or minus - operator.

A.difference(B)

A - B

Complement

The complement of a set A contains exactly those elements under consideration that are not in A. This is the equivalent to logical operation NOT.

$A^c=U-A$

Set Symmetric Difference

Set Symmetric Difference returns the set of all elements in either A or B, but not both. This is the equivalent to logical operation XOR.

In Python, we can use either symmetric_difference() method or caret ^ operator.

A.symmetric_difference(B)

A ^ B

Set Identities

Having the set operations, we can now show the most important set identities.

We will use the following example for all the set identities. You can change the elements on these example sets.

Identity laws

$ A \cup \varnothing = A $

$ A \cap \mathbb{U} = A $

Domination laws

$ A \cup \mathbb{U} = \mathbb{U} $

$ A \cap \varnothing =\varnothing $

Idempotent laws

$ A \cup A = A $

$ A \cap A = A $

Double Negation / Complementation / Involution law

$ \neg \left ( \neg A \right ) = A $

Commutative laws

$ A \cup B = B \cup A $

$ A \cap B = B \cap A $

Associative laws

$ A \cup ( B \cup C) = (A \cup B ) \cup C $

$ A \cap ( B \cap C) = (A \cap B ) \cap C $

Distributive laws

$ A \cap ( B \cup C) = (A \cap B ) \cup (A \cap C ) $

$ A \cup ( B \cap C) = (A \cup B ) \cap (A \cup C ) $

De Morgan’s laws

$ \neg \left ( A \cup B \right ) = \left ( \neg A \cap \neg B \right ) $

$ \neg \left ( A \cap B \right ) = \left ( \neg A \cup \neg B \right ) $

Absorption laws

$ A \cup ( A \cap B) = A $

$ A \cap ( A \cup B) = A $

Complement laws

$ A \cup \neg A = \mathbb{U} $

$ A \cap \neg A = \varnothing $

$ \neg \varnothing = \mathbb{U} $

$ \neg \mathbb{U} = \varnothing $

References

Set Operations

Cartesian Product

Powerset

Drawing Venn Diagrams in Python

Writing Set notation in Latex:

last update: Aug 2022

Cite this tutorial as: Teknomo.K. (2022) Set Theory in Python http://people.revoledu.com/kardi/tutorial/Python/

See Also: Python for Data Science

Visit people.Revoledu.com for more tutorials in Data Science

Copyright © 2022 Kardi Teknomo

Permission is granted to share this notebook as long as the copyright notice is intact.