What is Dictionary in python and how to use Dictionary in python

What is dictionary in python and how to use dictionary in python:-

Hello friend through this article I will tell you about what is dictionary in python and I will also tell you about how to use dictionary in python so read the article carefully.

what is dictionary in python and how to use dictionary in python
what is dictionary in python and how to use dictionary in python

What is dictionary in python?

Guys as usual everyone use dictionary I their day to day life it doesn’t matter it is oxford dictionary or another dictionary and also in python dictionary is use to find value of anything or anyone such as what is camera, what is anything else you want to search you can also understand dictionary in python as a key value pair dictionary is made to improve working speed or make your work easy in python such as you have to make dictionary in this way you can also try my all steps by making and running them so fist a fall when you declare dictionary you have to write like:

 

d1 = {}


After writing d1 = {} your dictionary will be automatically declared by python, you can also check that you dictionary id declared or not by typing this


Print(type(d1))

 

After writing this you have to run the program

If you get <class ‘dict’> in the result then it is prove that your dictionary is created successfully you have to always learn symbol in python because symbols can change class of any text or code like if you write

what is dictionary in python and how to use dictionary in python
what is dictionary in python and how to use dictionary in python


d1 () so you dictionary will not create because you have to put curly braces after d1, so you have to learn all the symbols which you get during learning python.

d1 = {} is a blank dictionary that mean there is nothing stored in a dictionary.

Now I am telling you about how to make complete dictionary in python, I will tell you it with an example so read example carefully.

I am making dictionary of 3 alphabets and what are their favorite fruits so lets began:


Let we make dictionary first as

d1 = {“A”:”Mango”}

After writing that our diary have a person and also have its value you can understand it as “A” is a person whose favorite fruit is Mango.

To check is your dictionary is correct or not you can check it by writing this

 

d1 = {“A”:”Mango”}

print(d1[“A”])

what is dictionary in python and how to use dictionary in python
what is dictionary in python and how to use dictionary in python


After writing this when you run the code you will get result as Mango this mean your code I correct and your dictionary is working correctly but dictionary is also case sensitive this mean if you write print(di[“a”]) instead of print(d1[“A”])and run the code it will show error so always write name in same method.

It is dictionary but it only contains one person and value now I am telling you about how to write or store multiple persons and their value and through this you can also find what you have saved value for dedicated person by this simple method.


Before writing the code you need to know all the persons and their value to which I am using in my dictionary.

Now there are person A, B, C in which favorite fruit of A is Mango

Favorite fruit of B is Banana

Favorite fruit of C is Grapes

 

what is dictionary in python and how to use dictionary in python
what is dictionary in python and how to use dictionary in python


Now I am filling all the information given in the dictionary.

 

d1 = {“A”:”Mango”, ”B”:”Banana”, ”C”:”Grapes”}

 

Now you can check your data by typing this code:-

 

d1 = {“A”:”Mango”, ”B”:”Banana”, ”C”:”Grapes”}

 

print(di[“A”])

by running this code you will get the result Mango which mean now you can find value of anything or any person whom you include in your dictionary as in include person “A” in my dictionary and when I try to find it I get the result Mango which mean value of “A” is Mango by this way you can make your dictionary with the help of python.

You can also remove any person from your dictionary easily

This function of dictionary is used when you want to delete any one word from many words without finding it manually then you can take help with this function of dictionary in python.

what is dictionary in python and how to use dictionary in python
what is dictionary in python and how to use dictionary in python


Now thing you want to delete “A” person from your dictionary and you don’t know where “A” is located in your dictionary and you want to delete it immediately by the simple process.

Here is your code which contain “A” also in it

 

d1 = {“A”:”Mango”, ”B”:”Banana”, ”C”:”Grapes”}

Now you want to delete it then you have to make a code as


d1 = {“A”:”Mango”, ”B”:”Banana”, ”C”:”Grapes”}

del d1[“A”]


after writhing this you need to check is your code works properly that mean is person “A” is deleted from your dictionary or not and to check this you need to print your dictionary and run the code by the simple process

 

d1 = {“A”:”Mango”, ”B”:”Banana”, ”C”:”Grapes”}

del d1[“A”]

print(d1)


And you have to run the code if you are unable to find person “A” in your dictionary so that you have successfully deleted one character from your dictionary.

Leave a Comment