Python · live now

Sets

An unordered collection with no duplicates: adding and removing elements, iterating, union, intersection and difference, and testing whether one set sits inside another.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

6

lessons

49

practice questions

44

prerequisite lessons

See it taught

1 of the 6 lessons here is free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3
a = {1, 2, 3} b = {3, 4} print(a | b, a & b)

Editor Output

1

Output:

{1, 2, 3, 4} {3}

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 6 lessons

The 6 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

Sets

12 questions

Understand the unique, unordered nature of Python sets, how to create them from various iterables, and how to determine their size using the len() function.

Covers

Sets overview

Creating a set

Length of a set

02

Add and remove elements from a set

8 questions

Learn how to add elements to a set and remove them using .remove() or .discard(), understanding how each method handles missing elements and duplicates.

Covers

Add elements to a set

Remove elements from a set

03

Iterating over a set

4 questions

Learn how to use a for loop to access each element in a Python set, noting that the elements may appear in any order due to the set's unordered nature.

Covers

Iterating over a set

04

Set union, intersection and difference

Preview

12 questions

Learn how to use the union, intersection and difference operations to combine, compare and subtract sets in Python using both methods and operators.

Covers

Set union

Set intersection

Set difference

Preview this lesson - no account needed

05

Subsets and supersets

8 questions

Learn how to determine whether one set is a subset or superset of another, including proper subsets and supersets, and use Python operators to perform these set comparisons.

Covers

Subsets

Supersets

06

Activity: nature reserve

5 questions

Practise using Python lists and sets to track unique animal species, update records, find overlaps and differences between groups, and perform calculations with animal data in a nature reserve context.

Covers

Unique animals

Update animals in Zone A

Shared species

High-priority species

Weighing the animals

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 6 red dots are the lessons in Sets; the 44 blue dots feeding into them are the lessons Sets depends on; the 50 pale dots are the rest of the learning path, which come after Sets or alongside it.SetsFirst lessons on the left100 lessons

Hover any dot to name its lesson.

6 lessons in this unit

44 prerequisite lessons across the unit, counting every step back to the start

50 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start Sets

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee

More in Data structures: Lists, Tuples, Dictionaries