Computer Science Grade 8 20 min

Dictionaries: Key-Value Pairs

Introduce dictionaries as collections of key-value pairs. Learn how to create and access values using keys.

What you'll learn

  • Identify the key and value in at least three different key-value pairs within a given dictionary example.
  • Explain, in their own words, the relationship between a key and its corresponding value in a dictionary.
  • Create a dictionary with at least four key-value pairs to represent information about their favorite video game or book.
  • Apply their understanding of dictionaries to retrieve a specific value from a pre-built dictionary when given its corresponding key, with 80% accuracy.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define what a dictionary is and explain its purpose in programming. Identify and differentiate between keys and values within a dictionary. Create dictionaries using appropriate syntax to store related data. Access, add, update, and remove data from dictionaries using their keys. Explain the difference between dictionaries and lists and when to use each. Demonstrate how to check for the existence of a key in a dictionary. Ever tried to find a friend's phone number in a messy notebook? 🤯 What if you could instantly find it just by knowing their name? In this lesson, you'll discover dictionaries, a powerful data structure that lets you organize information like a real-world dictionary or a phonebook. You'll learn how to store and retrieve d...
2

Key Concepts & Vocabulary

TermDefinitionExample DictionaryA collection of data that stores information in 'key-value' pairs. It's like a real-world dictionary where each word (key) has a definition (value).In Python: `{'apple': 'a fruit', 'banana': 'a yellow fruit'}` KeyA unique identifier used to look up a specific value in a dictionary. Keys must be immutable (like numbers, strings, or tuples).In `{'name': 'Alice', 'age': 14}`, 'name' and 'age' are keys. ValueThe data associated with a specific key in a dictionary. Values can be any data type (numbers, strings, lists, or even other dictionaries).In `{'name': 'Alice', 'age': 14}`, 'Alice' and 14 are values. Key-Value Pai...
3

Core Syntax & Patterns

Creating a Dictionary Dictionaries are defined using curly braces `{}` with key-value pairs separated by colons `:` and pairs separated by commas `,`. Use this syntax to initialize a new dictionary, either empty or with initial data. Example: `my_dict = {'key1': 'value1', 'key2': 'value2'}` Accessing Values To retrieve a value, use square brackets `[]` with the key inside: `dictionary_name[key]`. This is how you 'look up' a value using its unique key, similar to how you use an index for a list, but with a meaningful key instead of a number. Example: `my_dict['key1']` Adding/Updating Items To add a new key-value pair or update an existing one, assign a value to a key: `dictionary_name[new_key] = new_value`. I...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
What is the final content of the `prefs` dictionary after this entire code block is executed? `prefs = {'user': 'jdoe'}; prefs['user'] = 'asmith'; del prefs['user']; prefs['id'] = 123;`
A.{'id': 123}
B.{'user': 'asmith', 'id': 123}
C.{'user': None, 'id': 123}
D.The code will error because you can't delete a key you just updated.
Challenging
You need to count the occurrences of each word in the sentence "go team go". Which dictionary correctly represents the word counts?
A.{'go': 1, 'team': 1}
B.{'go': 2, 'team': 1}
C.{'go': 1, 'team': 1, 'go': 1}
D.{'words': ['go', 'team', 'go']}
Challenging
Keys in a dictionary must be immutable, meaning they cannot change. Based on this rule, which of the following data types CANNOT be used as a dictionary key?
A.string, like `'name'`
B.number, like `101`
C.list, like `[1, 2]`
D.tuple, like `(1, 2)`

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Data Structures: Organizing Information

Computer Science for other grades

Frequently asked questions

What grade level is "Dictionaries: Key-Value Pairs"?

Dictionaries: Key-Value Pairs is a Grade 8 Computer Science lesson on ExcelOS.

What will I learn in Dictionaries: Key-Value Pairs?

You'll be able to: Identify the key and value in at least three different key-value pairs within a given dictionary example; Explain, in their own words, the relationship between a key and its corresponding value in a dictionary; Create a….

Is "Dictionaries: Key-Value Pairs" free to practice?

Yes. You can read the tutorial preview for free, and signing up for a free ExcelOS account unlocks the full tutorial and all practice questions with instant feedback.

How many practice questions are included with Dictionaries: Key-Value Pairs?

This lesson includes 25 practice questions across multiple difficulty levels, each with instant feedback and explanations.

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.