Don't like this style? Click here to change it! blue.css
We practiced several things, building a list in a program, making a clear function definition, and using assert to test our function.
Help Task 1: Edit this snippet then run the function (in the terminal):
help(factors)
. Whoa!
FizzBuzz Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
So last time we looked at lists and some nice ways of using them. Now I want to introduce a very different type
of data storage idea. The dictionary. It uses { }
braces to hold
key-value pairs. The idea of key-value pairs is very important so let's take a look at some
basics.
Mini-Task 2: Create a dictionary named hero
give it two keys:
name
and strength
. Print the hero's name by using the name
key.
You can write to a dictionary the same way you read from a dictionary: mydict[key] = new_value
.
Alter-Task 3: Change
the hero's name to banana
and print again.
Research Task 4: use the dir(variable_name_here)
trick to see what
methods a dictionary has. Use help(variable.method)
to read the documentation on a particular
method. (Try items
.)
So a dictionary is a way of making a collection of data that really belongs together. Let's take a person. What traits should our code know about a student?
Creation Task 5: Create a dictionary called student
and populate it
with some data that you think an administrator would want to know about a student.
One crucial skill with dictionaries is deciding if a key exists or not. Here is a reason why, you can easily overwrite data that perhaps you didn't want to:
Evaluate Task 6: What is the value of book_index["Alchemy"]
after the
above snippet?
If you want to detect if a key exists we have a new operator called in
. This is a nice way of
asking if a key is in our dictionary (or a value is in a list).
The same trick works in lists and strings!
Function Task 7: Refactor the above snippet, so that there is now a function called
is_in(check_me, from_me)
which is called three times. Now call it on a dictionary and a string.
A very cool use of a dictionary is as a histogram. Take a look at this snippet:
Concept Task 8: As a META task determine the letter distribution of the words in this task!
Use a dictionary whenever you want to save and retrieve data based on a key or identifier. Be aware that the keys are never sorted.
Extra Credit: Write code which does this histogram trick but which gives the results in the order of the values.
Just like lists, we'll want to programmatically visit every value in a dictionary every now and again. Here are four different ways:
Applied Task 9:In the next snippet I'm giving you a dictionary of departments and each department will have a dictionary of phone numbers. Take this data and write a "phone call" loop which goes through each department and "calls" each person (by printing to the screen something like "Called Samantha Jones of the Booger Department at 555-555-5555").
We can get rid of a dictionary key with the word del(whatever[key_here])
.
Do to Learn Task 10: Try it out. Take the departments
dictionary and
delete departments["Math"]
. Then try to print "Math".
Check it out:
Create a family tree. Feel free to have arrays of dictionaries for children which can themselves be dictionaries. Think it out creatively.