Dictionaries and 46 exercises: 20-22

In Python we can create dictionaries that store values, like what we do with lists, except that instead of using an index to refer to something in it, we use other values called keys.

An example of a dictionary looks like this:

translate = {“hello”:”hola”,”my”:”mi”,”name”:”nombre”,”is”:”es”}

In this case, hello, my, name, and is are called keys, and each key has a corresponding value: hola, mi, nombre, and es.

I read about this topic in the book How to Think Like a Computer Scientist: Learning with Python by Allen Downey, Jeffrey Elkner, and Chris Meyers, which I had mentioned before in this post. You can check it out here.

I used dictionaries to solve the exercises 20-22 of the 46 showed in the past posts.

In number 20 I had to create a program that would take a list of English words and return a list of those words in Swedish, like this:


The get() method I used in the loop has two parameters: the first one is the key of the dictionary, the second one is the value to return if said key is not found.


Number 21 was about determining the frequency in which each character of a string is used:


And 22 was to create an encoder & decoder of ROT-13 that would be able to decipher the following message:

Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!


3 thoughts on “Dictionaries and 46 exercises: 20-22

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s