An Introduction to Python Sets — Part III

A complete guide to standard set operations in Python

Chaitanya Baweja
The Startup

--

A Venn Diagram (Source : realpython.com [link])

“A set is a Many that allows itself to be thought of as a One.“ — George Cantor

Where we left

In the first part, we discussed how to define set objects in Python. We saw that elements in a set are unique and unordered. A set itself can be modified, but the elements contained in a set must be of an immutable type. In the second part, we discovered some of the standard data structure operations like size, adding and deleting elements, modifying a set.

In this part, we cover a whole host of set operations that are supported in Python. This includes operations like union, intersection, difference etc.

I highly recommend skimming through the previous two stories before starting with this one.

Operators vs Methods

As they are unordered, sets in Python can’t be indexed or sliced. However, Python provides a whole host of operations on set objects that generally mimic the operations that are defined for mathematical sets. Most of these operations can be performed by both operator or method.

--

--

Chaitanya Baweja
The Startup

Machine Learning Engineer | Python | Data Analytics | Economics | Physics