Python Dictionary
What is python dictionary:
In Python language, dictionary data type stores data without any sequence. In this, the data remains as a key:value pair.
When we have to access any data from this dictionary, we know the value through the key. From the example given below, we will understand the syntax of the dictionary in Python language.
python_dictionary = {‘key1′:’value1’, ‘key2’: ‘value2′,’key3′:’value3′,’key4′:’value4’}
The dictionary data type is quite different from the list data type. As I mentioned earlier, all the elements in the list data type are in a sequential manner. With this we can get the list data type of any location through their index value. Whenever you print a list, the elements inside it will be printed in the same order every time.
This is not the case in dictionary data type. If you print the same dictionary multiple times, the location of the value pair inside it may be different.
For this reason, the sort function, which was used in the list, cannot be used with the dictionary.
From an example below we will understand the dictionary in a little more detail. Let’s say you are buying books on five different subjects in a new semester. We will create a dictionary of the subject and the price of the book.
books_dictionary = {‘maths’:300,’python’:400,’communication’:500,’data science’:600,’structures’:700}
In the dictionary example above, the names of the subjects (Maths, Python, etc.) are of the kind, while the price of those books is value.
If we want to print the entire dictionary, the entire dictionary will be printed by the command below.
print(books_dictioinary)
>> ‘maths’:300,’python’:400,’communication’:500,’data science’:600,’structures’:700
But more of the actual use of the dictionary is to print the value using the key. Let’s understand this with the help of the example given below.
print(books_dictioinary[‘maths’])
>> 300
Recent Comments