0Pricing
Learn AI with Python · Lesson

Sets

Discover how to use sets for operations involving unique elements.

Sets is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 5. 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 Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Sets

Sets are unordered collections of unique items in Python. They are useful when you need to store unique elements and perform set operations like union, intersection, and difference.

In this lesson, you’ll learn how to create and manipulate sets, and perform common set operations.

Sets — illustration 1

2

Creating Sets

A set is created by placing items inside curly braces {}, separated by commas. For example:

# Creating a set
fruits = {"apple", "banana", "cherry"}
print(fruits)

3

4

Unique Elements in Sets

Sets automatically remove duplicate items. For example:

# Sets with unique elements
numbers = {1, 2, 2, 3, 4, 4}
print(numbers)  # Outputs: {1, 2, 3, 4}

5

Set Operations: Union

The union() method returns a set containing all elements from two sets:

# Union of sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))  # Outputs: {1, 2, 3, 4, 5}

6

Set Operations: Intersection

The intersection() method returns a set containing only the elements that are common to both sets:

# Intersection of sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.intersection(set2))  # Outputs: {3}

7

Set Operations: Difference

The difference() method returns a set containing elements in the first set but not in the second:

# Difference of sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.difference(set2))  # Outputs: {1, 2}

8

Adding and Removing Elements

You can add or remove elements from a set using add() and remove() methods:

# Adding and removing elements
fruits = {"apple", "banana"}
fruits.add("cherry")
fruits.remove("banana")
print(fruits)  # Outputs: {'apple', 'cherry'}

9

10

Common Mistakes with Sets

Here are some mistakes to avoid:

  • Trying to access set elements using an index (sets are unordered).
  • Adding mutable objects like lists to a set (raises an error).
  • Assuming sets maintain the order of elements.

11

What Did We Learn?

In this lesson, you learned:

  • How to create and manipulate sets in Python.
  • The uniqueness property of sets and common set operations.
  • How to add and remove elements from sets.

Great job! Let’s move to the next topic.

Sets — illustration 11

Frequently asked questions

Is the “Sets” lesson free?

Yes — the full text of “Sets” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Sets”?

Discover how to use sets for operations involving unique elements. You practise Learn AI with Python 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 Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 5, 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 Learn AI with Python lesson?

Yes. Every Learn AI with Python 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.

All lessons in this course

  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries
  5. Strings as Data Structures
← Back to Learn AI with Python