5 Levels of Using Sets in Python

Yang Zhou
TechToFreedom
Published in
5 min readNov 23, 2020

--

5 Levels of Using Sets in Python

Introduction

A set is an unordered collection of unique elements. It is one of the most core data structures in computer science. Like other programming languages, Python has built-in implementations of the set and its operation functions.

Since the features and manipulations of sets are based on the set theory, it’s different from other data structures and may be confusing for beginners. Error-prone programs will be produced easily by a developer who is not familiar with sets.

This article will indicate 5 levels of using the sets in Python and explain it from elementary to profound. After mastering the 5 levels, bugs will leave you away. 🎉

Level 0: Know the Fundamental Features of Sets

The nature of sets in Python is determined by the set theory in mathematics. The basic features are as following:

  • Elements of a set are unordered.
  • A set cannot contain duplicate elements.
  • There are 3 basic operations on sets: union, intersection and complement.

Therefore, a set in Python can only include immutable objects and duplicate elements will be removed.

--

--