Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #82 -- Improved the chapter on dictionaries. #107

Merged
merged 1 commit into from
Aug 10, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions python_introduction/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,57 +220,70 @@ You can find a list of all available list methods in this chapter of the Python

## Dictionaries

A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. The syntax to define a dictionary is:
A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. The syntax to define an empty dictionary is:

>>> {}
{}

This shows that you just created an empty dictionary. Hurray!

Now, try writing the following command:
Now, try writing the following command (try replacing your own information too):

>>> django_dolls = {'Dottie' : 15, 'Lottie' : 305, 'EmmyLou' : 17}
>>> participant = {'name' : 'Ola', 'country' : 'Poland', 'favorite_numbers' : [7, 42, 92]}

Don't be surprised with the weird names. Go to the link: http://hubpages.com/hub/50-Doll-Names to look for more cute doll names. :P Just Kidding (You should do this if and only if you have a lot of time).
With this command, you just created a variable named `participant` with three key-value pairs:

Above, you just created a variable named `django_dolls` with three key-value pairs. The key Dottie points to the value 15, Lottie points to the value 305, EmmyLou points to the value 17. Do you want to check? Type:
- The key `name` points to the value `'Ola'` (a `string` object),
- `country` points to `'Poland'` (another `string`),
- and `favorite_numbers` points to `[7, 42, 92]` (a `list` with three numbers in it).

>>> print(django_dolls['Dottie'])
15
You can check the content of individual keys with this syntax:

>>> print(participant['name'])
Ola

See, it's similar to a list. But you don't need to remember the index - just the name.

What happens if we ask Python the value of a key that doesn't exist? Can you guess? Let's try it and see!

>>> participant['age']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'age'

Look, another error! This one is a **KeyError**. Python is helpful and tells you that the key `'age'` doesn't exist in this dictionary.

When to use a dictionary or a list? Well, a good point to ponder on. Just have a solution in mind before looking at the answer in the next line.

- Do you just need an ordered sequence of items? Go for a list.
- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a dictionary.

Dictionaries are mutable like "lists" meaning that they can be changed after they are created. You can add new key/value pairs to the dictionary after it is created, like:

>>> django_dolls['Jilly'] = 67
>>> participant['favorite_language'] = 'Python'

Like the lists, using `len()` method on the dictionaries, returns the number of key-value pairs in the dictionary. Go ahead and type in the command:

>>> len(django_dolls)
>>> len(participant)
4

I hope it makes sense uptil now. :) Ready for some more fun with Dictionaries? Hop on the next line for some amazing things.
I hope it makes sense uptil now. :) Ready for some more fun with dictionaries? Hop on the next line for some amazing things.

You can use `del` command to delete an item in the dictionary which has particular. Say, if you want to delete the entry corresponding to the key 'Dottie', just type in the following command:
You can use the `del` command to delete an item in the dictionary. Say, if you want to delete the entry corresponding to the key `'favorite_numbers'`, just type in the following command:

>>> del django_dolls['Dottie']
>>> django_dolls
{'Jilly': 67, 'EmmyLou': 17, 'Lottie': 305}
>>> del participant['favorite_numbers']
>>> participant
{'country': 'Poland', 'favorite_language': 'Python', 'name': 'Ola'}

As you can see from the output, the key-value pair corresponding to 'Dottie' key has been deleted.
As you can see from the output, the key-value pair corresponding to 'favorite_numbers' key has been deleted.

Apart from this, you can also change a value associated with an already created key in the dictionary. Type:

>>> django_dolls['Jilly'] = 100
>>> django_dolls
{'Jilly': 100, 'EmmyLou': 17, 'Lottie': 305}
>>> participant['country'] = 'Germany'
>>> participant
{'country': 'Germany', 'favorite_language': 'Python', 'name': 'Ola'}

As you can see, the value of the key 'Jilly' has been altered from *67* to *100*. :) Exciting? Hurrah! You just learnt another amazing thing.
As you can see, the value of the key `'country'` has been altered from `'Poland'` to `'Germany'`. :) Exciting? Hurrah! You just learnt another amazing thing.

### Summary

Expand Down