Enumerations – Practical Use

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

Enumerations – Practical Use

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. (1,2) + (3,4) returns?
A [1, 2, 3, 4]
B (1, 2, 3, 4)
2. (1,2) * 3 returns?
A (1, 2, 1, 2, 1, 2)
B (3, 6)
3. t = (1,2,2,3)\nt.count(2) returns?
A 2
B 1
4. t = (10,20,30)\nt.index(20) returns?
A 1
B 20
5. How many methods do tuples have?
A Many like lists
B Just 2: count() and index()

Match the Pairs

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

Left columnRight column
1t.append(4) AOutput: (1, 2, 1, 2)
2t.remove(1) BError: Tuples are immutable — no remove method
3(1, 2) + (3, 4) CError: Tuples have no append method
4t.count(2) DOutput: [1, 2, 3]
5(1, 2) * 2 EOk: Count how many times 2 appears in t
6sorted((3, 1, 2)) FValid: Concatenate two tuples into one

Definitions

Concatenation (+)

Fill in the Blanks

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

1.
t = (1, 2) _ (3, 4)
2.
t = (1, 2) _ 3
3.
t = (1, 2, 2, 3) print(t._____(2))
4.
t = (10, 20, 30) print(t._____(20))
5.
t = (3, 1, 2) print(______(t))

Spot the Error

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

Question 1
A t = (3, 1, 2)
B t.sort()
C print('Sorted:', t)
D print('Done')
Question 2
A t = (1, 2, 3)
B t += 4
C print('Tuple:', t)
D print('Done')
Question 3
A t = (1, 2, 3)
B idx = t.index(5)
C print('Index:', idx)
D print('Done')
Question 4
A t = (3, 1, 2)
B result = sorted(t)
C print('Sorted:', result)
D # expects tuple
Question 5
A t = (1, 2) + [3, 4]
B label = 'Combined'
C print('Combined:', t)
D print('Done')

Answer Key

Enumerations – Practical Use
Section A: Quick Questions
Q1
B — (1, 2, 3, 4)
Q2
A — (1, 2, 1, 2, 1, 2)
Q3
A — 2
Q4
A — 1
Q5
B — Just 2: count() and index()
Section B: Match the Pairs
Q1
t.append(4) → Error: Tuples have no append method
Q2
t.remove(1) → Error: Tuples are immutable — no remove method
Q3
(1, 2) + (3, 4) → Valid: Concatenate two tuples into one
Q4
t.count(2) → Ok: Count how many times 2 appears in t
Q5
(1, 2) * 2 → Output: (1, 2, 1, 2)
Q6
sorted((3, 1, 2)) → Output: [1, 2, 3]
Section C: Definitions
Q1
Concatenation (+) — Combines two tuples into a new tuple.~~Repetition (*)
Section D: Fill in the Blanks
Q1
t = (1, 2) + (3, 4)
Q2
t = (1, 2) * 3
Q3
t = (1, 2, 2, 3) print(t.count(2))
Q4
t = (10, 20, 30) print(t.index(20))
Q5
t = (3, 1, 2) print(sorted(t))
Section E: Spot the Error
Q1
B — Tuples don't have sort(). Use sorted() and convert back.
Q2
B — Must concatenate with a tuple: (4,) not just 4
Q3
B — index() raises ValueError if value isn't in the tuple
Q4
B — sorted() returns a list. Wrap in tuple() to get a tuple back.
Q5
A — Can't concatenate tuple + list. Both must be the same type.