Computer Science
Grade 6
20 min
Dictionaries
Dictionaries
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a dictionary is in Python.
Explain the concept of a key-value pair.
Create a new dictionary with at least two key-value pairs.
Access a value in a dictionary using its key.
Add a new key-value pair to an existing dictionary.
Update the value of an existing key in a dictionary.
Ever wonder how your phone instantly finds a friend's number just by their name? 📱 Dictionaries in Python work in a very similar way!
In this lesson, you will learn about a super useful Python data type called a dictionary. Dictionaries let us store connected pieces of information, like a person's name and their age, or a word and its definition. This is a powerful way to organize data in your programs.
Real-World Applications
Storing user profiles (usern...
2
Key Concepts & Vocabulary
TermDefinitionExample
DictionaryA collection of items stored in key-value pairs. Dictionaries are created using curly braces {}.student = {'name': 'Leo', 'grade': 6}
KeyThe unique identifier for an item in a dictionary. It's like a word in a real dictionary.In {'name': 'Leo'}, the key is 'name'.
ValueThe data associated with a key. It's like the definition of a word in a real dictionary.In {'name': 'Leo'}, the value is 'Leo'.
Key-Value PairThe combination of a key and its value, separated by a colon (:).In the dictionary {'name': 'Leo', 'grade': 6}, the first key-value pair is 'name': 'Leo'.
Curly Braces {}The special symbols used to start a...
3
Core Syntax & Patterns
Creating a Dictionary
variable_name = {key1: value1, key2: value2}
To create a dictionary, use curly braces {}. Inside, list your key-value pairs, with a colon : between the key and value, and a comma , separating each pair.
Accessing a Value
variable_name[key]
To get the value for a specific key, use the dictionary's variable name followed by the key inside square brackets [].
Adding or Updating a Value
variable_name[key] = new_value
To add a new item, provide a new key in square brackets and assign it a value. To update an existing item, use its key in square brackets and assign it a new value.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You have a dictionary of students and their test scores: `scores = {'Mia': 85, 'Leo': 92}`. Which code snippet would correctly represent adding a new dictionary for a new student, 'Ava', who has scores in two subjects?
A.scores['Ava'] = {'math': 95, 'science': 88}
B.scores['Ava'] = ['math': 95, 'science': 88]
C.scores.add('Ava', {'math': 95, 'science': 88})
D.scores['Ava']['math'] = 95
Challenging
Given this nested dictionary: `class_pet = {'name': 'Leo', 'details': {'type': 'Lizard', 'age': 2}}`. How do you access the value 'Lizard'?
A.class_pet['details', 'type']
B.class_pet['details']['type']
C.class_pet['type']
D.class_pet.get('details').get('Lizard')
Challenging
A classmate wrote this code to get a character's name, but it has an error. What is the mistake?
`character = {'name': 'Zelda', 'items': 5}`
`print(character('name'))`
A.The key 'name' should not be in quotes.
B.The print function cannot be used with dictionaries.
C.Parentheses () were used instead of square brackets [] to access the key.
D.The dictionary was created incorrectly.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free