Hi, this is our seventh video on Python tutorial and it is about dictionary data type. And dictionary is an unordered collection of items. It is a key value pair, and it is created by using curly braces just like double quintuple. It is only just values however in dictionary there is a key and value pair. We can create empty dictionary by using dictionary by using curly braces. Let's see this.
Just created the dictionary and it did not throw any error. Let's check the type is E it's a dictionary created Can also be created using integer and integer keys. The keys would be like in teacher it could be a text or string, or it could be any other data type. Let's see. So this is created and again when we go about checking the type, let's print out what it is what is in this dictionary to be able and see this is what we got. So let's check out the other mixed keys to the subject with mixed keys you just created Additionally it is.
The key is string bird a string to bed. Is this train the keys on the back, and it has a list within it. Let's check out. Let's see. Another, we can also create a dictionary using the built in dictionary function, just like the variables that we discussed earlier. Okay, and here, but dictionary for equals rotate, I can create the dictionary using the built in dish dish function.
Let's check the output. So see, and file dictionary is having each item is a pair, like n square in round brackets. Let's do it. Here's the dictionary is created using dictionary five and built in function. This lets you see, the output are exactly the same when when we did not create a pair and when we create a pair you can see However, these are different syntaxes through which you can create dictionaries. Let's delete this.
So, okay, now how can we access the elements from a dictionary just by using square brackets and identifying the key like in dictionary for we have a second key we have bought so let's print out What is the output for this we get the ball or we can also get access the value or elements within the dictionary by using the get method. And the benefit of using get method is instead of throwing Aaron when the key does not exist, it says that the key is not found. Okay, let's check out here. When I get this printed, I get Apple because on the first key, it's Apple, and when I go about checking this third key that does not exist, that see what output we get, we get nothing. So this is the difference between the get method and checking it using just the square brackets and putting the in key inside it.
We can also change the values of elements within the dictionary, just by identifying that value, accessing that value and giving it another assignment. Here, like for example, be assigned age as 26. In dictionary five variable, we exist the dictionary five variable with square brackets and putting the key that is age and giving it a new assignment of 27. And when we print it out, we'll get we get 27. Okay. Okay, let's delete this.
So now we can also print the dictionary values by using the print function. Check out see the values have been printed. On first it's Apple number two is bought and third is aged register and dictionary five we can add elements to a dictionary purchased defining its key and adding and assigning value to it. And here we just did it and when we print dictionary five again so we have that address or the new key value pair added to to the main dictionary. So I hope now you have a better understanding of dictionary if I may have any queries or questions feel free to contact me and subscribe. And like this video and if you are looking to create any video or automation topic or any specific topic, you can want it.
Thank you