This exercise is Part 1 of 4 of the birthday data exercise series. The other exercises are: Part 2, Part 3, and Part 4.
For this exercise, we will keep track of when our friend’s birthdays are, and be able to find that information based on their name. Create a dictionary (in your file) of names and birthdays. When you run your program it should ask the user to enter a name, and return the birthday of that person back to them. The interaction should look something like this:
Happy coding!
We covered dictionaries in a previous exercise, but it’s been long enough that we will review them here and add a few more notes.
The main topic for this week is the concept of a dictionary. In Python they are called dictionaries, but in other languages they may be called maps or hashmaps. The concept is the same: it is a way to use a key to access some value. It is mapping one set of data (the keys) to another set of data (the values). The only way to access the value part of the dictionary is to use the key.
As an example, grocery store systems can store the prices for various items by using a dictionary. In Python, it would look something like this:
You specify the dictionary between the {}
. Key / value pairs are specified as list (separated by commas). The key is on the left side, and the value is on the right side. Notice how I split up the dictionary definition on multiple lines - I did that just for convenience. You can also write the dictionary on the entire line, but it is hard to read (and it probably even goes off your browser window screen):
Now that we have the dictionary, we can query it (ask it) for the values associated with each key (for the prices associated with each food). We do that using brackets ([]
). For example, to get the price of a banana:
To get a list of all the keys, use the .keys()
method:
We have created our dictionary with many items in it, but we can also add items to it one by one. It is almost like accessing an element in the dictionary, except instead of using the element, we assign something to it.
Not all key / value pairs have to be the same. You can do something like:
Of course, this can get messy quickly, so be careful!
Dictionary keys must be unique. If you try to add an element to your dictionary with the same key, it will overwrite the value previously at that key:
If you try to ask the dictionary for a key that doesn’t exist, it will throw a KeyError
in your console, or your program will crash:
We will talk about how to “catch” errors in later exercises, but for now, there is another way to make sure you don’t throw an error. Instead of accessing elements in a dictionary with the []
notation, you can instead use the .get()
method. As the second argument to .get()
, you write down what the default value is for the dictionary: if there is no key like the one you wanted, what the dictionary should return:
This can be a helpful way of avoiding causing your program to crash.
Not all Python objects can be keys to a dictionary. For something to be a key in a dictionary, it must be immutable. Basically, it means that you can’t change that variable somewhere else. So for example, numbers and strings can be keys in a dictionary, but lists cannot. Because you can .append()
to a list, it cannot be a key in your dictionary. Lists can be values, just not keys:
Lastly, if we want to check if a particular key is in our dictonary, it is very easy. Just use the in
keyword:
These checks can be used together with if
statements to make more complex programs.
We’ve talked about strings a lot on this blog:
But we want to introduce one more optional concept for this exercise related to string formatting. There are a number of ways to format strings in Python, so I am just going to show you one quick way for a scenario you find yourself in often while programming.
A common scenario is like this: you want to print both a string and a number in the same line using one print()
statement. You can solve this problem like so:
But it gets tedious to use +
and str()
. Instead, you can use the .format()
method to cast (i.e. transform) your number into a string when it gets printed.
What we are doing is substituting the symbol {}
in the print statement in the string we want to display in the exact place we want the number to go, and use the .format()
to pass variables to the {}
that appear in order. What happens is the variables a
and b
get converted into strings automatically and injected into our print
statement cleanly. You can do this with floats, lists, dictionaries, or anything else you want to display.
There are a number of different formatting options if you want to get specific about how many decimal points to display, etc., but that is out of the scope of this exercise. If you want to read more about string formatting in Python, you can read about it on this helpful website that goes into a great amount of detail.