Skip to content

Advanced Python · Week 4

Sets

Step 1 of 5

Exercise 1: Introduction to Sets

Welcome to Python 2, Lesson 4! Today, we'll learn about sets, another useful data structure in Python.

What is a Set?

A set is an unordered collection of unique elements. This means that each item in a set appears only once, and the order doesn't matter.

Here's how to create a simple set:

# Create a set of fruits fruits = {"apple", "banana", "cherry"} print(fruits) print(type(fruits))

Now it's your turn! Create a set called colors with the following items:

  • red
  • green
  • blue
  • red (yes, add it twice!)

After creating the set, print it and its type to see the result. Notice how duplicates are automatically removed!

Starter code
# Create the colors set here

# Print the set and its type

Write your code here