Dictionary Operations ====================== Ex: Web application Facebook Registration Name Mail DOB Mobile Register Facebook from local computer ==========> Database Name Mail DOB Mobile XYZ xyz@m 22-3 xxxxxx -> Dictionary is a collection which can use to store the data in the format of key-value pairs. Syntax: dictionary-name = {key1 : value1, key2: value2, key3: value3,...} Properties: =========== 1) Is a pre-defined/built-in datatype with the class name of "dict". 2) Dictionary is the collection of items, that each item is the key-value pair. 3) All keys of the dictionary must be "unique". (No duplication is allowed for the keys) If any key of the dictionary is duplicated: then: the duplicated key must be upgraded with the latest definition. 4) Dictionary is ordered data structure. 5) Values of the dictionary may or may not be unique. 6) Keys in a dictionary are of any type. 7) Values in a dictionary are of any type. 8) No other collections like: list, tuple etc., allowed as keys in a dictionary. 9) Any collection we can consider as value in a dictionary. 10) Dictionary is not an index based. 11) Slicing is also not possible. 12) Dictionary elements can allow to access using keys. 13) Dictionary is mutable. d = {'a':11,'b':12,'c':13,'d':14} d1 = {'p':101,'q':111,'r':121,'p': 97} d2 = {'x':11,'y':11,'z':11} d3 = {True:'pqr',1-2j:2-3j,1.23:False,12:'p'} # d4 = {[1,2,3]:12,(1,4,5,8):'pqrst'} d4 = {'p':[1,3,5,7,9],'q':(1,2,3,4,5)} print(type(d)) print(d) print(d1) print(d2) print(d3) print(d4) # print(d[0]) print(d['c']) print(d1['r']) print(d2['z']) print(d3[True]) print("Before the change in the dictionary:") print(id(d)) print(d) d['c'] = 1321 print("After the change in the dictionary:") print(id(d)) print(d) Adding of new elements into dictionary: ======================================= Syntax: dictionary-name[key] = value; # WRITE A PROGRAM TO ENTER NAME AND PERCENTAGE IN A DICTIONARY AND DISPLAY INFROMATION ON # THE SCREEN. # take an empty dictionary record = {} # take count for number of students n = int(input("Enter number of students:")) # 5 # looping to enter student data to the record one after another. i = 1 # initialization for while loop while i <= n: name = input("Enter Student name:") marks = input("Enter % of Marks of Student:") record[name] = marks # can help to make dictionary element i = i + 1 print("Name of the student","\t","Marks of the student") for x in record: print("\t",x,"\t\t\t\t",record[x]) ======================================================= Dictionary Methods: =================== eval(): ====== -> can use to define the dictionary in the run time. Syntax: dictionary-name = eval(input("Enter some dictionary:")) for creating the empty dictionary: 1) {}: ===== 2) dict(): ========== # d1 = eval(input("Enter a dictionary:")) # # print(d1) d2 = {} d3 = dict() print(d2) print(d3) print(type(d2)) print(type(d3)) 3) len(): ======== -> to find the length, len() can be used. Syntax: len(dictionary-name) 4) get(): ========= -> can use to get the corresponding values based on the given keys. Syntax: dictionary-name.get(key) -> get() can used to defined some default value for any key. If the specified key in get() is already existed: get() can return "original value" where as the specified key is not exist: get() can return its default value Syntax: dictionary-name.get(key, default-value) d = {'apple':121,'banana':101,'cherry':321,'papaya':98,'mango':324} print(d.get('apple')) print(d.get('banana')) print(d.get('cherry')) print(d.get('papaya')) print(d.get('mango')) y = d.get('kiwi',123) x = d.get('apple',212) print(x) print(y)