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 in a MicroPython dictionary:
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 scripts that use dictionaries. So, take some time to get familiar with dictionaries now, because you’ll use them a lot later.
You will need:
Complete this tutorial first:
You will understand how dictionaries use key-value pairs for storing and managing data. This will help you better understand and work with the MicroPython code 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!):
In MicroPython, a dictionary is a collection of key-value pairs enclosed by curly braces. 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 scripts 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:
Here’s an example of a statement that creates a dictionary and names it info.
info = {'name': 'Sir Lancelot', 'score' : 1000}
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 script can use a key to look up a value like this:
number = info['score']
Since the value paired with the 'score' key is 1000, the number variable will now store 1000.
A script can change a value that’s paired with a key like this:
info['score'] = 1234
Keep in mind that 1234 could also be a variable that contains 1234. Like this:
number = 1234
info['score'] = number
Adding a key is almost the same as changing a key’s value. The only difference is that you have to 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.
info['goal'] = 2000
To see how the dictionary looks after the changes and additions, just print it.
print("info = ", info)
The dictionary will always keep each value with its key. But, it might totally rearrange the key-value pairs. For example the print statement might look like this. All the keys and values are correct, but their order is different. That’s fine because you will always use the key to look up or change a value.
info = {'goal': 2000, 'score': 1234, 'name': 'Sir Lancelot'}
Let’s try this in a script.
# first_dictionary from microbit import * sleep(1000) info = {'name': 'Sir Lancelot', 'score' : 1000} print("info = ", info) number = info['score'] print("number = ", number) info['score'] = 1234 print("info = ", info) info['goal'] = 2000 print("info = ", info)
Let's make a simple interactive dictionary with two key-value pairs.
# interactive_dictionary from microbit import * sleep(1000) string = input("Enter your name: ") text = input("Enter a recent game score: ") value = int(text) info = { } info['name'] = string info['score'] = value print("Dictionary") print("info = ", info) name_val = info['name'] text = info['score'] score_val = int(text) print("Hello", name_val, "your score was", score_val)
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.
The script 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.
string = input("Enter your name: ")
text = input("Enter a recent game score: ")
value = int(text)
This statement creates an empty dictionary and names it info.
info = { }
Since the dictionary does not already have 'name' or 'score' keys, these two statements add 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.
info['name'] = string info['score'] = value
The first print statement is just a heading. The second one displays info = and then the contents of the dictionary. Continuing the Sir Lancelot example, it would look like this: info = {'name': 'Sir Lancelot', 'score' : 1000}
print("Dictionary") print("info = ", info)
The first two statements 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.
name_val = info['name'] text = info['score'] score_val = int(text)
After looking up the values and storing them in variables, the script uses a print statement to display “Hello Sir Lancelot your score was 1000”.
print("Hello", name_val, "your score was", score_val)
Earlier, you used statements to add key-value pairs that were not already in the dictionary. If a dictionary already has a given key, those statments can be used to change the value that’s paired with the key.
print("Enter game high score instead: ") text = input("Enter high score: ") value = int(text) info['score'] = value print("Updated dictionary: ") print("info = ", info)
Pay close attention to the difference between the first and second time the script executes this statement:
info['score'] = value
The first one in the script added the key-value pair 'score' : 1000 to the info dictionary. The second one changed that key value pair to 'score' : 1234.
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'}
text = input("Enter score to reach next week: ") value = int(text) info['goal'] = value print("Updated dictionary") print("info = ", info) text = info['score'] score = int(text) text = info['goal'] score_goal = int(text) difference = score_goal - score print("That's", difference, "points more.")
Look what happened to the key-value pairs. The MicroPython runtime interpreter changed the order when you added the goal': 2000 pair. Again, that change in order did not matter because the script used the 'score' and 'goal' keys to look up the values that got stored in the score and score_goal variables. Then, the script used these values to correctly calculate:
difference = score_goal - score
= 2000 - 1234
= 766.
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/computer-microbit-talk
[2] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-basics
[3] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-data
[4] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-navigation-control-keyboard
[5] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-tilt-control
[6] https://python.microbit.org/v/2