← Python

Sets – Advanced (frozen sets, set comprehensions)

A set comprehension builds a set in one expression; frozenset() creates an immutable version of a set

What it does: Curly braces with a single expression (no :) creates a set comprehension. frozensets are hashable and can be used as dictionary keys. frozenset() creates a set that cannot be changed after creation - like a tuple vs list. Frozen means locked - no add(), remove(), or any mutation methods. Mutable objects can't be hashed - use a frozenset if you need a set as a key. x % 2 == 0 is True for even numbers - 0, 2, 4, 6, 8 from range(10). Same curly brace syntax but dict uses key: value pairs while set uses single values. frozensets support all read operations like union and intersection - just no mutations.

Common mistakes: Square brackets make a list comprehension. Use curly braces {} for a set comprehension. Frozen sets are immutable — they have no add() method.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
{x**2 for x in range(4)} creates?
{0, 1, 4, 9}[0, 1, 4, 9]
Matches
❌ frozenset({1,2}).add(3)
Error: frozenset is immutable — no add method
Applying
Meanings
Term
Set comprehension
Meaning
{expression for item in iterable} builds a set concisely.
Practice
squares = {x**2 for x in range(6)}
Testing
Gaps
squares = {x**2 x in range(5)}
Check Answer
Spots
1numbers = [1, 2, 3, 4, 5]
2squares = [x**2 for x in numbers]

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Angelfish' badge — Reef Perfectionist, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. {x**2 for x in range(4)} creates?
A. {0, 1, 4, 9} ✓
B. [0, 1, 4, 9]
Set comprehensions use curly braces with a single expression - the result is a set, not a list
2. What is a frozenset?
A. A sorted set
B. An immutable set ✓
frozenset() creates a set that cannot be changed after creation - like a tuple vs list
3. Can you add to a frozenset?
A. No it's immutable ✓
B. Yes
Frozen means locked - no add(), remove(), or any mutation methods
4. Can a frozenset be a dict key?
A. No
B. Yes it's hashable ✓
Dict keys must be hashable - frozensets qualify because they can't change
5. Can a regular set be a dict key?
A. No sets are mutable ✓
B. Yes
Mutable objects can't be hashed - use a frozenset if you need a set as a key

Match the Pairs

frozenset({1,2}).add(3) Error: frozenset is immutable — no add method
[x for x in range(5)] Error: This is a list, not a set comprehension
{x**2 for x in range(5)} Valid: Set comprehension
frozenset({1, 2, 3}) Ok: Create an immutable frozenset
len({1, 2, 2, 3}) Output: 3
{x for x in range(6) if x%2==0} Output: {0, 2, 4}

Key Term

Set comprehension
{expression for item in iterable} builds a set concisely.~~frozenset

Fill in the Blanks

1. squares = {x**2 for x in range(5)}
2. evens = {x for x in range(10) if x % 2 == 0}
3. fs = frozenset( {1, 2, 3} )
4. unique = set(['a', 'b', 'a'])
5. d = {frozenset({1, 2}): 'pair'}

Spot the Error

Question 1
A numbers = [1, 2, 3, 4, 5]
B squares = [x**2 for x in numbers]
C print('Squares:', squares)
D print('Done')
Line B — Square brackets make a list comprehension. Use curly braces {} for a set comprehension.
Question 2
A fs = frozenset({1, 2, 3})
B fs.add(4)
C print('Frozen:', fs)
D print('Done')
Line B — Frozen sets are immutable — they have no add() method.
Question 3
A key = frozenset({1, 2})
B d = {{1, 2}: 'pair'}
C print('Dict:', d)
D print('Done')
Line B — Regular sets are mutable and can't be dict keys. Use frozenset({1, 2}) as the key instead.
Question 4
A numbers = range(10)
B evens = {x for x in numbers if x % 2 = 0}
C print('Evens:', evens)
D print('Done')
Line B — Use == for comparison in conditions, not = (which is assignment).
Question 5
A unique = set('hello')
B word = 'hello'
C print(unique) # expects {'hello'}
D print('Done')
Line A — set('hello') splits into individual characters. Use {'hello'} for a set containing the whole word.
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?