Dictionaries
Dictionaries
Description:A dictionary is an unordered set of key-value pair. Each key is separated from its value by a
colon (:), and consecutive items are separated by commas. The entire items in a dictionary are enclosed
in curly brackets ({ }).
1) Demonstrate the different ways of creating Dictionary objects with suitable example programs.
d = {'PYTHON PROGRAMMING':99}
print(type(d))
print(d)
d = {'Happiness':99,'depends':100,'upon ourselves':98}
print(type(d))
print(d)
Output: <class 'dict'>
d = dict()
print(type(d))
d=eval(input("Enter Dictionay:"))
print(d)
print(type(d))
Output:
Enter Dictionay:{'a':100,'b':200,'c':300}
{'a': 100, 'b': 200, 'c': 300}
<class 'dict'>
2) Demonstrate the following functions/methods which operates on dictionary in Python with suitable
examples: i) len() ii)clear() iii) get() iv) pop() v)popitem() vi)keys() vii)values() viii)items() xi)copy()
x)update()
2
ii) clear():
This function is used to remove all entries from the dictionary.
d={100:"India",200:"capital",300:"Delhi"}
print(d)
d.clear()
print(d)
Output: {100: 'India', 200: 'capital', 300: 'Delhi'}
{}
iii) get(): It is used tTo get the value associated with the specified key. There are two forms of get()
method is available in Python.
a) d.get(key): If the key is available then returns the corresponding value otherwise returns None.It wont
raise any error.
d=dict({100:"India",200:"Delhi"}) #It creates dictionary with speci
fied elements
print(d.get(100))
Output: India
b)d.get(key,defaultvalue): If the key is available then returns the corresponding value otherwise returns
default value.
iv) pop():It removes the entry associated with the specified key and returns the corresponding value.
d.pop(key)
d={100:"India",200:"Delhi",300:"Mumbai"}
print(d)
print(d.pop(100))
print(d)
print(d.pop(200))
print(d)
Output:
India
{200: 'Delhi', 300: 'Mumbai'}
Delhi
{300: 'Mumbai'}
v) popitem(): It removes an arbitrary item(key-value) from the dictionaty and returns it.
d={100:"karthi",200:"saha",300:"sri"}
print(d)
print(d.popitem())
print(d.popitem())
print(d)
Output:
d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
print(d.keys())
for key in d.keys():
print(key)
d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
print(d.values())
for key in d.values():
print(key)
Hyderabad
Mumbai
Delhi
viii)items(): It returns list of tuples representing key-value pairs like as shown below.
[(k,v),(k,v),(k,v)]
d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
list = d.items()
print(list)
d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
d1=d.copy()
print(d1)
print(d)
Output: {100: 'Hyderabad', 200: 'Mumbai', 300: 'Delhi'}
x)update():
d.update(x)
All items present in the dictionary 'x' will be added to dictionary 'd
d={100:"Hyderabad",200:"Mumbai",300:"Delhi"}
d1 ={'a':'pearls', 'b':'corals'}
d.update(d1)
print(d)
Output :
{100: 'Hyderabad', 200: 'Mumbai', 300: 'Delhi', 'a': 'pearls', 'b':
'corals'}
3) Write a Python program to find number of occurrences of each letter present in the given string
word=input("Enter any word: ")
d={}
for x in word:
d[x]=d.get(x,0)+1
for k,v in d.items():
print(k,"occurred ",v," times")
Output: Enter any word: mississippi
m occurred 1 times
i occurred 4 times
s occurred 4 times
p occurred 2 times
4) Write a Python program to find number of occurrences of each vowel present in the given string.
word=input("Enter any word: ")
vowels={'a','e','i','o','u'}
d={}
for x in word:
if x in vowels:
d[x]=d.get(x,0)+1
for k,v in sorted(d.items()):
print(k,"occurred ",v," times")
Output:
Enter any word: Facetious
a occurred 1 times
e occurred 1 times
i occurred 1 times
o occurred 1 times
u occurred 1 times
5) Write a program to accept student name and marks from the keyboard and creates a dictionary. Also
display student marks by taking student name as input.
n=int(input("Enter the number of students: "))
d={}
for i in range(n):
name=input("Enter Student Name: ")
marks=input("Enter Student Marks: ")
d[name]=marks # assigninng values to the keys of the dictionary 'd
'
while True:
name=input("Enter Student Name to get Marks: ")
marks=d.get(name,-1)
if marks== -1:
print("Student Not Found")
else:
print("The Marks of",name,"are",marks)
option=input("Do you want to find another student marks[Yes|No]")
if option=="No":
break
print("Thanks for using our application")
for x in d:
print("\t",x,"\t",d[x])# display information on the screen.