Tutorial 8, Data Types: Dictionaries

In this lesson, we will talk about dictionaries. Dictionaries are like lists, but instead of having the elements organized by index, elements have keys.

Let's say we have information about a person. We can do it with a list.

example: person: John; last name: Green; he was born in 1998 and was born in Canada.

Let's say we receive this information from a web server. Will we know what those informations are?
We wouldn't be right because it would be very confusing. So in that case, it will be much better to have a dictionary.
To start a new dictionary, we use curly braces{}.
Each element of a dictionary is always composed of a key-value pair. So in this case, we need a key, which could be first_name, and the value we had, which was John. Now a comma, to start the second element of the dictionary. Then get the type of the variable "person." And we get a dictionary. Okay, let's visualize this below:

Now it is much clearer what those pieces of information are.
If we want to get the first information about the person, we can just type person and use braces [], but instead of using index, we are going to use the key first_name.


Dictionaries are used in several programming languages, but normally they have different names, like an object in JavaScript or an associative array in PHP. 
Dictionaries in Python are also mutable, so we can change the value of a property. Let's try that:


We can also add new keys to the dictionary:

And now we just added a new key-value pair (element). And the keys in the dictionary are also called properties. So we just added a new property to this dictionary.
The values of this dictionary are mostly strings. We only have one number, but those values can be of any data type. We can have stings, numbers, lists, tuples, and even dictionaries.
So let's add a new property called children. John has a daughter called Nathalie and a son called Ethan.


As a challenge for you, John just had another baby called Ana. Can you add her to the list of John's children? Just try it and get back here after you do it.
Alright:


We just added Ana to the list of John's children using the append method.
Now let's see what happens when we try to get a property that does not exist!

As you can see, we didn't get the age because there is no property called age, so we got an error. Which would be very bad if it was in the middle of our program, but to avoid that, we can use the get function.
And if it finds it, it is going to get the value associated with that function; if not, we can return a message like an invalid property. 

And now it just returned the value associated with this property. 
Now something really useful to know: let's say we have a variable called key, and associated with this variable, we have a string. We can get that property because doing that is pretty much the same as using the first method. Let's have a look:

This is really useful, and we are going to use it a lot in the future. To finish this lesson, let me show you how to erase all information from a dictionary.


We can move on to the next lesson now. If you have any questions, don't forget to leave them in the comment section. Thank you, good luck, and happy coding!


















No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...