✦ Python Foundations Collections module

Concept 10 · Sets

Keep values unique.
Do not rely on order.

Sets remove duplicates and are useful when membership matters more than position.

01 Learn02 Try03 Predict

A set keeps unique values without indexes.

Sets remove duplicates and are useful when membership matters more than position.

tags = {"python", "django", "python", "ai"}
print(len(tags))

The repeated value appears only once in a set.

Try changing one value, then explain which part of the collection changed and why.

↳

Keep this in mind: Read the collection operation before predicting the result.

Try it
tags = {"python", "django", "python", "ai"}

print(len(tags))
print("django" in tags)

tags.add("data")
print("data" in tags)

Run the example, then change one value and observe the result.

Python Playground

Loading Python runtime…

Edit the example and run it in your browser.

sets.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read each operation before choosing an answer.

a = {1, 2, 3}
b = {3, 4, 5}

print(len(a & b))
print(len(a | b))
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Ask Sloby about this concept

Describe what feels unclear and Sloby will guide your reasoning.

Hi! I'm Sloby. Let's explore this Python concept together.

What will this program print, and why? Explain duplicates and membership.

visitors = ["Ana", "Marko", "Ana", "Jelena"]
unique_visitors = set(visitors)

print(len(visitors))
print(len(unique_visitors))
print("Ana" in unique_visitors)
Python AI Tutor · Learn by understanding, not just by running code.