Language

Data Structures · Lesson 10 of 56

Sets

Source: 3-Data Structures/3.3-Sets.ipynb

Start here — no coding background needed

What you will learn

Keep unique items only — no duplicates.

In simple words

Sets drop duplicates automatically. Great for unique tags, unique visitors.

Think of it like this

Guest list where same person signing twice still counts once.

Ways to store many values together — shopping lists, contacts, unique items.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Sets

Sets are a built-in data type in Python used to store collections of unique items. They are unordered, meaning that the elements do not follow a specific order, and they do not allow duplicate elements. Sets are useful for membership tests, eliminating duplicate entries, and performing mathematical set operations like union, intersection, difference, and symmetric difference.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5}
<class 'set'>

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
<class 'set'>

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5, 6}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5, 6}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 4, 5, 6, 7}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 4, 5, 6, 7}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
{2, 4, 5, 6, 7}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
set()

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
True
False

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{4, 5, 6}
{4, 5, 6}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5, 6}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{7, 8, 9}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 7, 8, 9}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
False
True

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{1, 2, 3, 4, 5}

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
{'tutorial', 'we', 'discussing', 'this', 'In', 'about', 'sets', 'are'}
8

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Conclusion

Sets are a powerful and flexible data type in Python that provide a way to store collections of unique elements. They support various operations such as union, intersection, difference, and symmetric difference, which are useful for mathematical computations. Understanding how to use sets and their associated methods can help you write more efficient and clean Python code, especially when dealing with unique collections and membership tests.

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

s = {1, 2, 2, 3}. Print len(s) — should be 3 unique items.

Python