#8 Python :- Dictionaries
Dictionary:-
It is basically key value pair . They are optimized to know the value when the key is known. Creating a dictionary is very simple just placing the elements inside the curly brakets {} separated by commas.
e.q. mydic= {"monu":"good", "madhu":"lazy", "sonu":"fatty"}
Properties :-
i) Mutable:- It means we can change the values of dictionaries.
e.q. :- mydic= {"monu":"good", "madhu":"lazy", "sonu":"fatty"}
mydic["monu"] ="cute"
print(mydic) --->{'monu':'cute', 'madhu':'lazy', 'sonu':'fatty'}
We can also add and delete key value pair by same method:-
mydic = {"monu":"good", "madhu":"lazy", "sonu":"fatty"}
mydic["manish"] ---> "skiny"
print(mydic) ----> = {'monu':'good', 'madhu':'lazy', 'sonu':'fatty', 'manish':'skiny'}
del mydic["sonu"]
print(mydic) ----.{'monu':'good', 'madhu':'lazy', 'manish':'skiny'}
ii) Unordered :- we can't do indexing and slicing for the dictionaries.
Some project:-
Project 1:- Create a dictonary asking some key value and showing the result.
Soln:- dic={'monu':'good', 'madhu':'lazy', 'sonu':'fatty', 'manish':'skiny'}
word= input('Enter the key word:- ')
print(dic[word])
Comments
Post a Comment