« Middle School Courses
This course is included with our Coding/STEAM Curriculum - Middle School Plan

Lesson Plan

Lesson: Dictionaries

Time: 40+ mins

Introduction

In this lesson, students will use Python dictionaries to store a collection of values accessible by name.

Vocabulary

  • Dictionary: A dictionary is a data structure that stores values that are referenced by some unique name, a key. Elements in a dictionary can be accessed using the key that refers to the value that the user wants. Here is what the syntax looks like:

  • Key: A unique value like a number or a string that has some relation to the value that is stored with the key.
  • Key-value Pair: A key-value pair is made of a unique key that is associated with a value. For example,

Objectives

Studnets will...
  • Create and alter values stored in dictionaries
  • Identify differences between a key and a value in a key-value pair
  • Access values and keys in a dictionary
  • Add and remove elements from dictionaries
  • Iterate through dictionaries

Materials

  • Computers (1 per student) with student account access to Tynker.com

Warm-Up (5 minutes)

Ask students to describe or provide an example for at least three of the following terms:
  • List
  • String
  • “For” loop
  • Nested loop
  • Mutable
  • Function
  • Parameter

Activities (35 minutes)

Facilitate as students complete the Dictionaries modules on their own:
1. Dictionaries (Tutorial)
    • Students will read a short document that explains dictionaries in Python.
    • Optional: Ask students, “True or false: A “key” is a unique value (like a number of string) that has some relation to its value. (True)
    • Are students struggling with the “Translation dictionary” puzzle? Give a hint: Remind students that each “key” should correspond to a string value (e.g., its translation to another language). If students are still struggling, help them get started by providing them with the first line of their program. It might look something like this:
      definitions = {
    • Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
2. Assessing a Value (Tutorial)
    • In this module, students will read a short document that explains how values can be accessed from a dictionary by adding the key in square brackets after the dictionary.
    • Optional: Encourage students to experiment with the code in the provided examples. For example, ask students to replace the given states (i.e., California, Illinois, Alaska) with their favorite states.
    • Are students struggling with the “Famous computers” puzzle? Give a hint: Tell students to use code from the first example as a reference.
3. Adding Key-Value Pairs (Tutorial)
    • In this module, students will read a short document that explains how assigning a value to a dictionary adds the key-value pair to the dictionary.
    • Are students struggling with the “Factorial dictionary” puzzle? Give a hint: Tell students to use a “while” loop and brainstorm possible solutions with a neighbor.
4. Dictionary Iteration (Tutorial)
    • In this module, students will read a short document that explains how a “for” loop iterates on a dictionary by setting the iterator variable to each key in the dictionary.
    • Are students struggling with the “Print the winner” puzzle? Give a hint: Tell students to use an “if” statement with a comparison operator.
    • For the adventurous coder, encourage them to find an alternate solution (e.g., solve the puzzle with only one state variable) for the “Print the winner” puzzle.
5. Keys and Values (Tutorial)
    • In this module, students will read a short document that explains keys and values.
    • Optional: Before students select the “play” button in the first example, ask them to write down what they think the code will output.
    • Are students struggling with the “Opponents” puzzle? Encourage students to use functions such as “matchup.keys()” and “matchup.values()” and try using a “for” loop.
    • Are students struggling with the “Average score” puzzle? Direct their attention to the provided hint.
6. Quiz
    • This quiz requires students to apply concepts from this lesson to solve 3 different puzzles. There are no multiple choice questions.

Discussion Questions/Follow-Up Activities (20 minutes)

Lead a discussion with your students:
    • What is a dictionary?
    • Who can describe how they used dictionaries today in Python?
    • True or False: A “key” is a unique value like a number or a string that has some relation to the value that is stored with the key. (True)
    • What was the most challenging puzzle for you today? What problem-solving steps did you take?
    • Who would like to share a success story that happened during today’s lesson?

US Standards

  • CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
  • CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
  • CSTA: 2-AP-11, 2-AP-12, 2-AP-13, 2-AP-14, 2-AP-15, 2-AP-17, 3A-AP-14, 3A-AP-17, 3B-AP-11, 3B-AP-12
  • CS CA: 6-8.AP.11, 6-8.AP.12, 6-8.AP.13, 6-8.AP.14, 6-8.AP.15, 6-8.AP.17, 9-12.AP.12, 9-12.AP.14, 9-12.AP.16
  • ISTE: 1.c, 1.d, 4.d, 5.c, 5.d
This course is not part of your plan. Please upgrade to view all answer keys

Class Presentations

These student-facing slide presentations help educators seamlessly run Tynker lessons in a virtual or physical classroom setting. Each lesson has its own set of slides that introduce the big ideas, suggest unplugged activities, and include a section for each activity module. While running lesson slides, you can switch back and forth between the activity, the slides, answer keys and other lesson materials.
A sample slide presentation is available for your review. Please log in to view all the class presentations available with your plan..
Lesson 1
Welcome to Python
32 Slides
Lesson 2
Variables and I/O
29 Slides
Lesson 3
Data Types
29 Slides
Lesson 4
Math Operators
32 Slides
Lesson 5
Boolean Logic
35 Slides
Lesson 6
Turtle Graphics
31 Slides
Lesson 7
Branching
27 Slides
Lesson 8
While Loops
27 Slides
Lesson 9
Strings
32 Slides
Lesson 10
Lists
28 Slides
Lesson 11
For Loops
24 Slides
Lesson 12
Functions
28 Slides
Lesson 13
Dictionaries
20 Slides
Lesson 14
Classes and Objects
24 Slides
Lesson 15
Recursion
20 Slides