Sets – Advanced (frozen sets, set comprehensions)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Sets – Advanced (frozen sets, set comprehensions)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. {x**2 for x in range(4)} creates?
A [0, 1, 4, 9]
B {0, 1, 4, 9}
2. What is a frozenset?
A A sorted set
B An immutable set
3. Can you add to a frozenset?
A No it's immutable
B Yes
4. Can a frozenset be a dict key?
A Yes it's hashable
B No
5. Can a regular set be a dict key?
A No sets are mutable
B Yes

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1frozenset({1,2}).add(3) AOk: Create an immutable frozenset
2[x for x in range(5)] BValid: Set comprehension
3{x**2 for x in range(5)} COutput: 3
4frozenset({1, 2, 3}) DOutput: {0, 2, 4}
5len({1, 2, 2, 3}) EError: frozenset is immutable — no add method
6{x for x in range(6) if x%2==0} FError: This is a list, not a set comprehension

Definitions

Set comprehension

Fill in the Blanks

Complete each line of code by filling in the missing parts.

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

Spot the Error

Shade in the letter of the line that contains an 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')
Question 2
A fs = frozenset({1, 2, 3})
B fs.add(4)
C print('Frozen:', fs)
D print('Done')
Question 3
A key = frozenset({1, 2})
B d = {{1, 2}: 'pair'}
C print('Dict:', d)
D print('Done')
Question 4
A numbers = range(10)
B evens = {x for x in numbers if x % 2 = 0}
C print('Evens:', evens)
D print('Done')
Question 5
A unique = set('hello')
B word = 'hello'
C print(unique) # expects {'hello'}
D print('Done')

Answer Key

Sets – Advanced (frozen sets, set comprehensions)
Section A: Quick Questions
Q1
B — {0, 1, 4, 9}
Q2
B — An immutable set
Q3
A — No it's immutable
Q4
A — Yes it's hashable
Q5
A — No sets are mutable
Section B: Match the Pairs
Q1
frozenset({1,2}).add(3) → Error: frozenset is immutable — no add method
Q2
[x for x in range(5)] → Error: This is a list, not a set comprehension
Q3
{x**2 for x in range(5)} → Valid: Set comprehension
Q4
frozenset({1, 2, 3}) → Ok: Create an immutable frozenset
Q5
len({1, 2, 2, 3}) → Output: 3
Q6
{x for x in range(6) if x%2==0} → Output: {0, 2, 4}
Section C: Definitions
Q1
Set comprehension — {expression for item in iterable} builds a set concisely.~~frozenset
Section D: Fill in the Blanks
Q1
squares = {x**2 for x in range(5)}
Q2
evens = {x for x in range(10) if x % 2 == 0}
Q3
fs = frozenset( {1, 2, 3} )
Q4
unique = set(['a', 'b', 'a'])
Q5
d = {frozenset({1, 2}): 'pair'}
Section E: Spot the Error
Q1
B — Square brackets make a list comprehension. Use curly braces {} for a set comprehension.
Q2
B — Frozen sets are immutable — they have no add() method.
Q3
B — Regular sets are mutable and can't be dict keys. Use frozenset({1, 2}) as the key instead.
Q4
B — Use == for comparison in conditions, not = (which is assignment).
Q5
A — set('hello') splits into individual characters. Use {'hello'} for a set containing the whole word.