[010] Introduction to Python Basics: None, Dictionary
5 min readJul 4, 2020
Remember the special data type “None” where some methods that works on lists didn’t really produce anything. It modifies the lists in place but the output that yielded is none. Most of the languages have something similar to this to represent the absence of value like the “Null”.
- For instance, the users don’t have any weapons when they are starting the game. In this case, the variable weapons may be assign with None.
- This is completely valid Python code and we are able to get None from it.
Dictionary:
- Let’s talk about the next data structure and this one is called a dictionary. While in other languages, it might be called the hash table, map or objects. In python, we have this idea of dictionary and referred as dict.
- Well, it’s a data type in Python but also a data structure. Data structure is the way for us to organize our data in a form which has different pros and cons.
- For example of what we seen before which is lists where it is easily ordered. We can access the lists through indexes like 0 and 1 in the square brackets. The lists is also able to reverse and slice which is really nice.
- This is how the dictionary look like and let’s decipher it. The curly brackets are used to denotes a dictionary and unlike list where we have key value pairs. A key is a string for us to grab the another value pairs.
- In order for us to access any of the values, we will able to get the key values pairs of 2 when we grab the b. So basically the Python will search for key b in memory and grab the value 2.
- Here, we grab the c which is not exist and then get an error in the result. The dictionary is an unordered key value pair and what do I mean?
- Unordered means that the key value pairs is not right next to each other in memory. Remember where the lists can be accessed with indexes like 0,1,2 and so on. The item in the lists is right next to each other and then the lists is ordered.
- All these key value like a, b and x is in different memory spot.
- When we return the entire dictionary, we might not have all the keys in order. The keys is scattered across our memory. When we get the result, we see that it is in order. This is happening because that the dictionary is small.
- If we have a huge dictionary, we might not able to get the keys in order. Thus, the dictionary is an unordered key value pair and as long as we know the key then our computer will know where to grab the values.
- The cool things about data structures where it is containers around data where the data can be anything we want.
- Here, we can notice that any values can be contained in the dictionary and it is still valid.
- We are able to get the values for the key that we entered for the dictionary.
- We are also able to get the second item/value for the key ‘a’ when we use the indexes of 1. This operation is almost similar to the lists.
- The lists is also able to obtain the information like the dictionary but with a different ways.
Then, you might think what is this? Why we need different data structures like lists and dictionary? Why can’t we just have one thing which is much more simpler?
- So a good programmer able to decide what data structure to be used and when and what is the trade-offs for the data structured used. Till now, we only learn lists and dictionary. There is tons of data structures out there.
- The ability to decide what data structures to use when faced with different scenarios. However this requires practice and experience.
So when to use lists and dictionaries?
- First, the dictionary is not sorted and list is ordered. For instance, we want the list of people and the line up customer in the shop. Then we probably will use lists.
- Whereas the items that doesn’t need order, we can use dictionary like the groceries list to buy during a trip. In addition, the dictionary holds more information by its own when compared to lists where lists.
- There are pros and cons as we learned more data structures and we will understand why? No worries.