hey guys welcome to the next blog and Python tutorial for beginners in this blog I'm going to show you how to use dictionaries in Python so first of all what are dictionaries so dictionaries in Python are like associative lists or a map now you can think of dictionary as a list of pairs so let me show you how to define a dictionary so you can define any variable name and to define a dictionary first of all you use these curly brackets and inside these curly brackets you provide a list of key value pairs so let's provide the list of key value pairs so first key is name and the name is max the second key is for example age and the age of Max is let's say 14 and we can also write here in which year he was born so we can just write here and he was born in 2004 for example and I'm going to press ENTER here and once again I'm going to just access the values inside this dictionary which is D and you can see our dictionary is created now so as I said dictionary is a list of key value pairs and all these values which you see here before this colon are called keys so name is a key here here is a key here and ade is a key here and whatever values you see after the colon are called values so max is a value 2004 is a value and 14 is a value and you can access the values from a dictionary based upon their keys so for example I have this dictionary D I can use the square bracket and the key name for example I want to get the name value I can just give the name key here and then press ENTER and it's going to return me the Associated value related to key name in the same way you can use other keys also for example age and I'm going to press ENTER and it's going to give me 14 which is the value so age is a key here and 14 is the value now what type of data types you can store in a dictionary so as key you can define any data type so let me define a new dictionary here and I'm going to give these curly brackets and as I said you can define a string value as key also you can define a number as a key for example 15 : 15 let's try the float values so I'm going to just write 15 0.1 : 15 0.1 this is also allowed in dictionary so you can use string values you can use integer values you can use decimal values as key and also let's try the boolean value so you can use the boolean values also as keys and you can use a couple also as a key so I'm going to just give a tuple two comma three and then I'm going to assign a value of five to this key and as values also you can use string values or decimal values or integer values or a boolean values or any kind of collection you can use as a value so I'm going to press ENTER here and now to access let's say we want to access the value for this key which is this tuple so I'm going to just enter this tuple inside these square brackets and it's going to return me five or I can just provide inside the square bracket true here and then press Enter it's going to return me the value which is associated with it which is true itself or I can just write here 15 and it's going to return me 15 in turn because 15 is associated with this value which is also 15 now what happens when a key is not there and we try to access it so I'm going to just access hundred from this a dictionary it's going to give us error that this key is not present in the dictionary you can also use the LAN method to find out the number of items in the dictionary and you can see it says five items are there in the dictionary II and we can count this item so this is one item two item three item for item and the fifth item is here or in other words you can also say that Elian function is going to return you the number of key value pairs which are stored in dictionary you can also use for example I'm going to use my D dictionary now and you can also use a method called get and then you can give the key name here in the parenthesis so let's say I want to get that value associated with the name key I can get it like this and it's going to give me the value associated with the name key you can also add a new key so this D dictionary you can see there are right now three key value pairs and I can add one more key value pair so to add a key value pair you need to just write D and in the square bracket you just need to give the name of the new key so I'm going to just write surname here which is the name of the new key in the dictionary D and then you need to give the value associated with that key so when I press ENTER and once again when I try to access the values inside the dictionary now you can see that surname is added to your dictionary now if you want to remove any key value pair from a list then you can use d dot pop method and then the name of the key which you want to remove let's say we want to remove the surname once again which we have added and you can just press enter and now you can just print the values inside D now you can see the surname key value pair is remove you can also use a clear function so let's see what's there in the e dictionary so these are the values inside dictionary I can use a dot clear - clear the values inside the dictionary so once again when I try to access e it will give me the empty dictionary also you can delete the dictionary using de el function and the name of the dictionary and when I press ENTER and when I try to access this dictionary e once again it's going to say that this name is not defined you can also update the values in a dictionary so this is my dictionary and I want to update the name for example so I can use the dictionary name and then the key here for example name and the new name I want to associate with this key is let's say a new name here and when I try to access this dictionary once again you can see the name is changed you can also use a method called update to update this key value pair so once again I want to change the key value pair which is name so I can give this curly bracket and then give the key value pair which I want to change so name and the new name will be max once again and once again when I try to access the dictionary it will give me this dictionary and you can see the name is updated now now there is a function in dictionary called keys which is used to list out all the keys of that dictionary so you can see it will list out all the keys of the particular dictionary there is also a function called values which will list out all the values of that dictionary if you want to list out all the key value pairs you can use the function called items here it will give you the key value pair list now the last function I want to show here is let me list out the content of the dictionary first of all so there is one more function which you can use here is which is pop item and you don't need to give any argument with this pop item when you press Enter it's going to remove the last key value pair which you have added or updated so here we have dated this name key value pair that's why this is removed and now when you try to see the content of the dictionary this name key value pair is removed so this is how you can use dictionaries in Python I hope you have enjoyed this blog I will see you in the next blog
Post a Comment