- Create an empty dictionary, add four keys and values to the dictionary. Print the keys from the dictionary.
dic={}
dic[‘O’]=”Oxyzen”
dic[‘Ca’]=”Calcium”
dic[‘Na’]=”Sodium”
dic[‘H’]=”Hydrogen”
print(dic)
print(dic.keys())
- Create a dictionary, dic={‘O’:’Oxyzen’,’Ca’:’Calcium’,’Na’:’Sodium’,’H’:’Hydrogen’}. Print the values from the dictionary.
dic={‘O’:’Oxyzen’,’Ca’:’Calcium’,’Na’:’Sodium’,’H’:’Hydrogen’}
print(dic)
print(dic.values())
- Create a dictionary, dic={‘O’:’Oxyzen’,’Ca’:’Calcium’,’Na’:’Sodium’,’H’:’Hydrogen’}. Print the items from the dictionary.
dic={‘O’:’Oxyzen’,’Ca’:’Calcium’,’Na’:’Sodium’,’H’:’Hydrogen’}
print(dic)
print(dic.items())
4.Create a dictionary marks={‘maths’:97,’science’:98,’history’:78}. Remove the key science from the dictionary.
marks={‘maths’:97,’science’:98,’history’:78}
del marks[‘science’]
print(marks)
- Create a dictionary marks and clear all the items from the dictionary.
marks={‘maths’:97,’science’:98,’history’:78}
marks.clear()
print(marks)
- Create a dictionary, delete the dictionary and check whether the dictionary exists or not.
marks={‘maths’:97,’science’:98,’history’:78}
del marks
print(marks) #Following Error is displayed :NameError: name ‘marks’ is not defined
- Write a program create an empty dictionary capitals. Add key,value to the dictionary. When the user enters the key it should display the value; if the key does not exist, should exit the program.
Enter the state : AP
Capital of AP is Hyderabad
Enter the state : KA
Capital of KA is Bangalore
Enter the state : PP
Key does not exist
capitals={
“AP”:”Hyderabad”,
“TS”:”Hyderabad”,
“TN”:”Chennai”,
“KA”:”Bangalore”,
“WB”:”Calcutta”
}
capitals[“MH”]=”Mumbai” #adding
capitals[“WB”]=”Kolkatta” #modifying
while True:
x=input(“Enter the state : “)
if(x in capitals): #Checking whether the key exist in the dictionary or not
print(“Capital of {} is {} “.format(x,capitals[x]))
else:
print(“Key does not exist”)
break
- Write a python program to remove the key from dictionary.
Initial dictionary {‘d’: 4, ‘c’: 3, ‘a’: 1, ‘b’: 2}
Enter the key to delete(a-d):c
Updated Dictionary : {‘d’: 4, ‘a’: 1, ‘b’: 2}
Enter the key to delete(a-d):t
Key not found!
d = {‘a’:1,’b’:2,’c’:3,’d’:4}
print(“Initial dictionary : “,d)
key=input(“Enter the key to delete(a-d):”)
if key in d:
del d[key]
print(“Updated Dictionary :”,d)
else:
print(“Key not found!”)
- Write a program to update dictionary d1 with d2.
Eg: d1={‘a’:1,’b’:2,’c’:3}
d2={‘e’:4,’f’:5}
d1={‘a’:1,’b’:2,’c’:3}
d2={‘e’:4,’f’:5}
d1.update(d2)
print(“Dictionary after update : “,d1)
- Write a program to Generate a Dictionary that Contains Numbers (between 1 and n). Take value n from the user and print the number and its square.
Enter a number:5
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
n=int(input(“Enter a number:”))
d={x:x*x for x in range(1,n+1)}
print(d)
- Write a program to sum all the values in a dictionary d={‘A’:101,’B’:40,’C’:976,’D’:2}
d={‘A’:101,’B’:40,’C’:976,’D’:2}
print(“Total sum of values in the dictionary is {}”.format(sum(d.values())))
12.Create a dictionary marks={‘maths’:97,’science’:98,’history’:78}. Print keys and values.
marks={‘maths’:97,’science’:98,’history’:78}
for subject,marks in marks.items():
print (subject,marks)
Visit www.mongofactory.com to know more about the trainer.