Dictionaries – Basics (creation, accessing), online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
Dictionaries – Basics (creation, accessing) in one minute
What students need to know
A dictionary stores data as key-value pairs inside curly braces: person = {'name': 'Alice'}. The key is the label used to look something up, and the value is the data stored under it. A value is accessed with square brackets around its key — person['name'] — which looks items up by name rather than by position, unlike a list.
Mistakes students make
- Using square brackets to create a dictionary —
person = ['name': 'Alice']— is a syntax error; dictionaries need curly braces{}. - Looking up a key that doesn't exist raises a
KeyErrorrather than returning nothing.
PREVIOUS · MODULE 36Lists – Methods (append, remove, pop, insert)
YOU ARE HERE · MODULE 37Dictionaries – Basics (creation, accessing)
NEXT · MODULE 38Dictionaries – Methods (keys, values, items, get)
What students will learn in this module
Every question in Module 37
Quick questions
- What is a dictionary?AAn ordered list of itemsBA collection of key-value pairs
- How do you create a dictionary?ACurly braces {}BSquare brackets []
- d = {'a': 1}\nprint(d['a'])\nWhat prints?A1B'a'
- What separates a key from its value?AA colon :BAn equals sign =
- Can dictionary keys be duplicated?AYesBNo each key must be unique
Key terms
Students type the meaning of:
DictionaryKeyValuedict[key]
Match the pairs
person = {'name': 'Ali'}
print(person['age'])Match it to what it doesd = {name: 'Alice'}Match it to what it doesperson = {'name': 'Ali', 'age': 20}Match it to what it doesd = {}
d['x'] = 10Match it to what it doesp = {'name':'Ali'}
print(p['name'])Match it to what it doesp = {}
print(type(p))Match it to what it doesFill in the blanks
person = _'name': 'Alice'_
print(person[______])
d = {} d[_______] = 'blue'pet = {'type'_ 'cat'}scores = {'math': 90_ 'english': 85}
Spot the error
Question 1
A# Create a person dictionaryBperson = ['name': 'Alice']Cprint('Name:', person['name'])Dprint('Done')
Question 2
A# Create a person dictionaryBperson = {'name' = 'Alice'}Cprint('Name:', person['name'])Dprint('Done')
Question 3
Aperson = {'name': 'Alice'}Bgreeting = person[name]Cprint('Hello,', greeting)Dprint('Done')
Question 4
Aperson = {'name': 'Alice'}Bgreeting = person('name')Cprint('Hello,', greeting)Dprint('Done')
Question 5
Aperson = {'name': 'Alice'}Blabel = 'Greeting'Cgreeting = person.nameDprint('Hello,', greeting)
