Lists + Dictionaries

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

Lists + Dictionaries

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. {1,2,3} | {3,4,5} returns?
A {3}
B {1, 2, 3, 4, 5}
2. {1,2,3} & {2,3,4} returns?
A {2, 3}
B {1, 2, 3, 4}
3. {1,2,3} - {2,3,4} returns?
A {4}
B {1}
4. {1,2,3} ^ {2,3,4} returns?
A {1, 4}
B {2, 3}
5. What does | do with sets?
A Union combines all items
B Intersection

Match the Pairs

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

Left columnRight column
1a = {1,2} b = [3,4] a | b AOutput: {1, 2, 3}
2s = {1,2,3} s.add([4,5]) BOk: Difference {1}
3a = {1,2,3} b = {2,3,4} a & b CError: Both must be sets for |
4a = {1,2,3} b = {2,3,4} a - b DValid: Intersection {2, 3}
5a={1,2} b={2,3} print(a | b) EError: Lists can't be set elements
6a={1,2} b={2,3} print(a & b) FOutput: {2}

Definitions

Union

Fill in the Blanks

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

1.
a = {1, 2, 3} b = {3, 4} print(a _ b)
2.
a = {1, 2, 3} b = {2, 3, 4} print(a _ b)
3.
a = {1, 2, 3} b = {2, 3} print(a _ b)
4.
a = {1, 2} b = {1, 2, 3} print(a.________(b))
5.
a = {1, 2, 3} b = {2, 3, 4} print(a _ b)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A a = {1, 2, 3}
B b = {3, 4}
C print(a + b)
Question 2
A a = {1, 2, 3}
B b = {2, 3, 4}
C print(a and b)
Question 3
A a = {1, 2, 3}
B b = {2, 3, 4}
C print(a or b)
Question 4
A a = {1, 2, 3}
B b = {3, 4}
C combined = a.union[b]
D print('Union:', combined)
Question 5
A print({1, 2}.subset({1, 2, 3}))
B a = {1, 2}
C b = {1, 2, 3}
D print('Done')

Answer Key

Lists + Dictionaries
Section A: Quick Questions
Q1
B — {1, 2, 3, 4, 5}
Q2
A — {2, 3}
Q3
B — {1}
Q4
A — {1, 4}
Q5
A — Union combines all items
Section B: Match the Pairs
Q1
a = {1,2} b = [3,4] a | b → Error: Both must be sets for |
Q2
s = {1,2,3} s.add([4,5]) → Error: Lists can't be set elements
Q3
a = {1,2,3} b = {2,3,4} a & b → Valid: Intersection {2, 3}
Q4
a = {1,2,3} b = {2,3,4} a - b → Ok: Difference {1}
Q5
a={1,2} b={2,3} print(a | b) → Output: {1, 2, 3}
Q6
a={1,2} b={2,3} print(a & b) → Output: {2}
Section C: Definitions
Q1
Union — Combines all items from both sets with no duplicates.~~Intersection
Section D: Fill in the Blanks
Q1
a = {1, 2, 3} b = {3, 4} print(a | b)
Q2
a = {1, 2, 3} b = {2, 3, 4} print(a & b)
Q3
a = {1, 2, 3} b = {2, 3} print(a - b)
Q4
a = {1, 2} b = {1, 2, 3} print(a.issubset(b))
Q5
a = {1, 2, 3} b = {2, 3, 4} print(a ^ b)
Section E: Spot the Error
Q1
C — Sets don't support + for union. Use | or .union().
Q2
C — Use & for set intersection, not 'and' (which is a boolean operator)
Q3
C — Use | for set union, not 'or' (which is a boolean operator)
Q4
C — Use parentheses () to call methods, not square brackets []
Q5
A — The method is issubset(), not subset()