LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Dictionary Primer

Dictionary Primer

What it’s about

This short tutorial introduces dictionaries, a way of organizing data as a set of key-value pairs. Shopping and ingredient lists are examples of key-value pairs. Here’s how one might look if you print it after converting it to a string:

shopping = { 'bananas': 3, 'bread': 1, 'peanut butter': 1, 'soda': 6 }

Another example could be a Cybersecurity app where a controller micro:bit sends radio navigation commands to a micro:bit in a cyber:bot. Both micro:bit modules will run projects that use dictionaries. So, take some time to get familiar with dictionaries now, because you’ll use them a lot later.

Before you start

You will need:

  • A micro:bit module (on or off a cyber:bot)
  • A USB A to MicroB cable
  • A computer with Internet access to makecode.microbit.org

Complete this tutorial first:

  • Get Started with micro:bit and MakeCode
  • Computer – micro:bit Talk

After you finish

You will understand how dictionaries use key-value pairs for storing and managing data. This will help you better understand and work with the Makecode block examples in the Cybersecurity: Radio Data and later tutorials.

After this, you will also be ready to move on to the next tutorials (with more coming soon!):

  • Radio Basics
  • Radio Data
  • Navigation Control from a Keyboard
  • Radio Tilt Control

About Dictionaries

In Makecode, a dictionary is a collection of key-value pairs. For example {‘name’: ‘Sir Lancelot’, ‘score’ : 1000} is a dictionary that contains two key-value pairs ‘name’: ‘Sir Lancelot’ and ‘score’ : 1000. Sir Lancelot is the string value that’s paired with the ‘name’ key, and 1000 is the int value paired with the ‘score’ key.

Storing information in key-value pairs really helps when one device collects the information and then sends it to another device that processes it. The device that processes the information can use keys to get the values it needs out of the dictionary it receives. 

Collections of key-value pairs are important for making data exchanges between devices more manageable. Phone and browser apps that communicate with servers make extensive use of key-value pairs. As you will soon see, it’s an essential ingredient in projects where one micro:bit sends data that another micro:bit uses to accomplish a robotic task.

The Cybersecurity tutorials make two micro:bit modules work together like a browser and server would, by using dictionaries. To get ready, this activity will guide you through some of the basics, practicing these operations on a dictionary:

  • Create a dictionary
  • Add key-value pairs
  • Use keys to access values

First Dictionary

Here’s an example of a block that creates a dictionary and names it info. 

The dictionary itself is {‘name’: ‘Sir Lancelot’, ‘score’ : 1000}. The dictionary has two key-value pairs. One is ‘name’: ‘Sir Lancelot’, and the other is ‘score’ : 1000}. The value paired with the ‘name’ key is ‘Sir Lancelot’. The value paired with the ‘score’ key is 1000.

 
A project can use a key to look up a value like this:

Since the value paired with the ‘score’ key is 1000, the number variable will now store 1000.

Adding a key to an already existing dictionary is easy. The only thing you have to do is use a key name that’s different from any of the keys that are already in the dictionary. The two keys already in the dictionary are ‘name’ and ‘score’. So, if you use a new name and assign it a value, a new key-value pair will appear in the dictionary.

Example project: first_dictionary

  • Connect your micro:bit module to your computer with its USB cable.
  • In a browser, go to makecode.microbit.org to open the micro:bit Makecode Editor.
  • Enter and name the first_dictionary project.
    (See Save & Edit Projects.) 
  • Flash the project into the micro:bit
    (See Flash Projects with MakeCode Editor.)

  • Go to Google Chromelabs Serial Terminal to check the results in the serial monitor.
    (See Use the Serial Monitor.)
  • Verify that:
    •  The initial dictionary print is info =  {‘score’: 1000, ‘name’: ‘Sir Lancelot’}
    • After changing the ‘score’ key’s value to 1234, the second dictionary print is: info =  {‘score’: 1234, ‘name’: ‘Sir Lancelot’}
    • After adding the ‘score’: 1234 key-value pair, the last dictionary print is: info =  {‘goal’: 2000, ‘score’: 1234, ‘name’: ‘Sir Lancelot’}

Create a Dictionary

Let’s make a simple interactive dictionary with two key-value pairs.

Example project: interactive_dictionary

  • Enter and name the project interactive_dictionary. 
  • Click the Download button.

  • If the serial monitor is not already open, go to Google Chromelabs Serial Terminal.
  • Make sure that the Local echo box is checked. 
  • Click to the right of the Enter your name: prompt.
  • Follow the prompts to enter a name and a score. 

Assuming you entered Sir Lancelot for the name and 1000 for the score, the terminal should respond with info = {‘name’: ‘Sir Lancelot’, ‘score’ : 1000} on one line and Hello Sir Lancelot your score was 1000 on the next.

  • Verify that the response in the serial monitor is consistent with your input.

How interactive_dictionary Works

The project starts by storing your keyboard input into the string and value variables. Let’s assume you typed Sir Lancelot and 1000 in response to the prompts. The string variable will store ‘Sir Lancelot’, and the value variable will store 1000. 

Since the dictionary does not already have ‘name’ or ‘score’ keys, this block adds two key-value pairs to the dictionary. Assuming the string variable stores ‘Sir Lancelot’ and the value variable stores 1000, one key-value pair would be ‘name’: ‘Sir Lancelot’, and the second would be ‘score’ : 1000.

The first serial write block is just a heading. The second one displays name: and score: then the contents of the dictionary. Continuing the Sir Lancelot example, it would look like this: name: Sir Lancelot score: 1000

The first two blocks here “look up” values in the dictionary by using their keys. The name_val variable stores the value that’s paired with ‘name’ in the dictionary. Continuing the earlier example, name_val would store the string value ‘Sir Lancelot’ and score_val would store the int value 1000.

After looking up the values and storing them in variables, the project uses a serial write block to display “Hello Sir Lancelot your score was 1000”.

Try This: Use Keys to Change Values

Earlier, you used blocks to add key-value pairs that were not already in the dictionary. If a dictionary already has a given key, there’s a block that can be used to change the value that’s paired with the key. 

  • Change the project’s name from interactive_dictionary to interactive_dictionary_try_this.
  • Add the blocks below to the end of the project.
  • Click the Download button.

  • In the serial monitor, click the the right of the Enter your name: prompt.
  • To make it match the example shown here, enter the same values that you did before, and then enter 1234 when prompted to enter high score.
  • Verify that the updated dictionary that gets displayed is info = {‘name’: ‘Sir Lancelot’, ‘score’: 1234}.

Did You Know?

This kind of dictionary isn’t alphabetical! The order that the key-value pairs are in doesn’t matter. You will always use a key to get the value paired with it.

For example, it doesn’t matter which one of these dictionaries you use, info[‘score’] still returns 1234.

info = {'name': 'Sir Lancelot', 'score': 1234}
info = {'score': 1234, 'name': 'Sir Lancelot'}

Your Turn: Expand and Use the Dictionary

  • Change project’s name from interactive_dictionary_try_this to interactive_dictionary_your_turn.
  • Add these blocks to the end of the project you’ve been building.
  • Click the Download button.

  • Click the the right of the Enter your name: prompt in the serial monitor.
  • To make it match the example shown here, enter the same values as before, and then enter 2000 when prompted to enter the score to reach next week.
  • Verify that the updated dictionary that gets displayed is info = {‘goal’: 2000, ‘score’: 1234, ‘name’: ‘Sir Lancelot’}.
  • Verify that it displays That’s 766 points more.

Look what happened to the key-value pairs. The Makecode runtime interpreter changed the order when you added the goal’: 2000 pair. Again, that change in order did not matter because the project used the ‘score’ and ‘goal’ keys to look up the values that are stored in the score and score_goal variables. Then, the project used these values to correctly calculate:

difference = score_goal - score
            = 2000 - 1234
            = 766

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/dictionary-primer_makecode/
Links