#dictionary can not be accessed vy index,but through key value
films = {"muthu":"one of the movie in which rajini comes in double role",
"basha":"Don of a don movie,a cult hit",
"enthiran":"rajini's scifi movie",
"murattu kalai":"movie taken in paganeri"}
print(films)
print(films["murattu kalai"])
#to add a new value to dictionay we dont have a method or function,but we can assign like the below one
films["kaala"]="more like a basha 2,april 27th - 2018 release"
print(films)
#if we use the same key and assign it will update instead of creating new element
films["kaala"]="probably thalaivars gonna be biggest hit"
print(films)
#similarly it will take the last set of key,value combination if there is any duplicate
films = {"muthu":"one of the movie in which rajini comes in double role",
"basha":"Don of a don movie,a cult hit",
"enthiran":"rajini's scifi movie",
"murattu kalai":"movie taken in paganeri",
"muthu":"one of the thalaivar & sarath babu combination movie"}
print (films["muthu"])
bike = {"make":"royal enfiled","model":"himalayan","cc":400,"review":"mostly avg or bad"}
print (bike["model"])
print (bike["cc"])
#delete a element or delete a entire dictonary or cleare the elements in the dictionary
del(bike["review"])
# del(bike)
# bike.clear()
print (bike)
#getting the unpresent key value will result in error,in that case we can use get function
# print (bike["color"])
print(bike.get("color"))
#while True is used to make the user keep qasking the questions,unless he types quit
# while True:
# x = input("enter any thalaivar's film name: ")
# if x == "quit":
# break
# description = films.get(x)
# print(description)
# #the below piece of code will return error if we enter any unknown key value
# print(films[x])
#same code is rewritten to print the custom message if the value doesnot exists in dictory
while True:
x = input("enter any thalaivar's film name: ")
if x == "quit":
break
if x in films:
description = films.get(x)
print(description)
print(films[x])
else:
print ("{} - doesnot exists in dictionaryy".format(x))
Saturday, 10 February 2018
Python Dictionaries part1 - Day 7
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment