Dictionaries – Basics (creation, accessing)

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

Dictionaries – Basics (creation, accessing)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a dictionary?
A An ordered list of items
B A collection of key-value pairs
2. How do you create a dictionary?
A Curly braces {}
B Square brackets []
3. d = {'a': 1}\nprint(d['a'])\nWhat prints?
A 1
B 'a'
4. What separates a key from its value?
A A colon :
B An equals sign =
5. Can dictionary keys be duplicated?
A Yes
B No each key must be unique

Match the Pairs

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

Left columnRight column
1person = {'name': 'Ali'} print(person['age']) AError: Keys need quotes if string
2d = {name: 'Alice'} BOutput: <class 'dict'>
3person = {'name': 'Ali', 'age': 20} CValid: Dict with two key-value pairs
4d = {} d['x'] = 10 DError: Key 'age' doesn't exist
5p = {'name':'Ali'} print(p['name']) EOutput: Ali
6p = {} print(type(p)) FOk: Add key to empty dict

Definitions

Dictionary

Fill in the Blanks

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

1.
person = _'name': 'Alice'_
2.
print(person[______])
3.
d = {} d[_______] = 'blue'
4.
pet = {'type'_ 'cat'}
5.
scores = {'math': 90_ 'english': 85}

Spot the Error

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

Question 1
A # Create a person dictionary
B person = ['name': 'Alice']
C print('Name:', person['name'])
D print('Done')
Question 2
A # Create a person dictionary
B person = {'name' = 'Alice'}
C print('Name:', person['name'])
D print('Done')
Question 3
A person = {'name': 'Alice'}
B greeting = person[name]
C print('Hello,', greeting)
D print('Done')
Question 4
A person = {'name': 'Alice'}
B greeting = person('name')
C print('Hello,', greeting)
D print('Done')
Question 5
A person = {'name': 'Alice'}
B label = 'Greeting'
C greeting = person.name
D print('Hello,', greeting)

Answer Key

Dictionaries – Basics (creation, accessing)
Section A: Quick Questions
Q1
B — A collection of key-value pairs
Q2
A — Curly braces {}
Q3
A — 1
Q4
A — A colon :
Q5
B — No each key must be unique
Section B: Match the Pairs
Q1
person = {'name': 'Ali'} print(person['age']) → Error: Key 'age' doesn't exist
Q2
d = {name: 'Alice'} → Error: Keys need quotes if string
Q3
person = {'name': 'Ali', 'age': 20} → Valid: Dict with two key-value pairs
Q4
d = {} d['x'] = 10 → Ok: Add key to empty dict
Q5
p = {'name':'Ali'} print(p['name']) → Output: Ali
Q6
p = {} print(type(p)) → Output: <class 'dict'>
Section C: Definitions
Q1
Dictionary — A collection of key-value pairs in curly braces {}.~~Key
Section D: Fill in the Blanks
Q1
person = {'name': 'Alice'}
Q2
print(person['name'])
Q3
d = {} d['color'] = 'blue'
Q4
pet = {'type': 'cat'}
Q5
scores = {'math': 90, 'english': 85}
Section E: Spot the Error
Q1
B — Dictionaries use curly braces {}, not square brackets []
Q2
B — Use colon : between key and value, not equals =
Q3
B — Key must be in quotes: 'name' is a string, not a variable
Q4
B — Use square brackets [] to access keys, not parentheses ()
Q5
C — Use bracket notation person['name'], not dot notation person.name