Sets
Learn about unique collections of elements.
Sets is a free Python For Kids lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python For Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Sets
Welcome to the Data Structures lesson! Today, we'll learn about sets in Python, which allow you to store unique collections of elements.

2
What are Sets?
Sets are collections of unique items. Unlike lists, sets do not allow duplicate elements. They are useful when you need to ensure that all items are unique.
- Unique Elements: No duplicates are allowed in a set.
- Unordered: Sets do not maintain the order of elements.
3
Creating a Set
You can create a set by placing items inside curly braces ({}), separated by commas, or by using the set() function.
Example:
# Creates a set of fruits using curly braces
fruits = {"apple", "banana", "cherry"}
# Creates a set of numbers using the set() function
numbers = set([1, 2, 3, 4, 5])4
Accessing Set Items
Since sets are unordered, you cannot access items by index. Instead, you can iterate through a set using a loop.
Example:
# Iterates through each fruit in the set and prints it
for fruit in fruits:
print(fruit)
5
Adding Items to a Set
You can add items to a set using the add() method.
Example:
# Adds a new fruit to the set
fruits.add("orange")
# Attempts to add a duplicate fruit (will have no effect)
fruits.add("apple")
# Prints the updated set
print(fruits) # Output might vary due to unordered nature
6
Removing Items from a Set
You can remove items from a set using the remove() or discard() methods.
Example:
# Removes 'banana' from the set
fruits.remove("banana")
# Removes 'grape' if it exists; does nothing otherwise
fruits.discard("grape")
# Prints the updated set
print(fruits) # Output might vary7
Set Operations
Sets support various operations like union, intersection, and difference, which allow you to combine or compare sets.
- Union: Combines all unique elements from both sets.
- Intersection: Retrieves elements common to both sets.
- Difference: Retrieves elements that are in one set but not the other.
Example:
# Creates two sets of numbers
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
# Union of set_a and set_b
union_set = set_a.union(set_b)
print("Union:", union_set) # Output: {1, 2, 3, 4, 5, 6}
# Intersection of set_a and set_b
intersection_set = set_a.intersection(set_b)
print("Intersection:", intersection_set) # Output: {3, 4}
# Difference of set_a from set_b
difference_set = set_a.difference(set_b)
print("Difference:", difference_set) # Output: {1, 2}
8
Example Program: Unique Visitors
Let's create a program that tracks unique visitors to a website using sets.
Code:
# Initializes an empty set to store unique visitor names
unique_visitors = set()
# List of visitors (including duplicates)
visitors = ["Alice", "Bob", "Charlie", "Alice", "Diana", "Bob"]
# Adds each visitor to the set
for visitor in visitors:
unique_visitors.add(visitor) # Adds the visitor name to the set
# Prints the total number of unique visitors
print("Total Unique Visitors:", len(unique_visitors)) # Output: 4
# Prints the names of unique visitors
print("Unique Visitors:", unique_visitors) # Output: {'Alice', 'Bob', 'Charlie', 'Diana'}
9
# Creates a set of numbers
numbers = {1, 2, 3, 4, 4, 5}
# Adds a new number to the set
numbers.add(6)
# Attempts to add a duplicate number
numbers.add(2)
# Prints the set
print(numbers)
10
Great Job!
You've learned how to use sets to store unique collections of elements in Python. Sets are powerful tools for managing data where uniqueness is important. Keep practicing by creating your own sets and experimenting with their operations!

Frequently asked questions
Is the “Sets” lesson free?
Yes — the full text of “Sets” is free to read here on the web, and the Python For Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Sets”?
Learn about unique collections of elements. You practise Python For Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python For Kids?
No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sets” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python For Kids lesson?
Yes. Every Python For Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.