Dictionaries: ============= strings ==> quotes 'python' or "python" or '''python''' str list ==> [] [1,2,3,4,5] list tuple ==> () (5,4,3,2,1) tuple sets ==> {} {2,4,6,8,10} set About Dictionaries: =================== ==> built-in class: dict # How to create the dictionary # empty dictionary d1 = {} # d2 = {1,2,3,4,5} # dictionary is the collection of elements # here: elements are the combination of key and value pair # syntax: key : value # Syntax of dictinary: # {key1:value1,key2:value2,key3:value3,...} # keys: #-===== # keys are strings or integers or floats or complex numbers or booleans # values are strings or integers or floats or complex numbers or booleans d2 = {'key1':'apple','key2':'banana','key3':'cherry','key4' : 'papaya'} d3 = {101:'a',201:'b',301:'c',401:'d'} d4 = {'a' : 1.001,'b' : 2.001,'c' : 3.001} # keys of the dictionary must be unique. But values are not unique. d5 = {'a' : 100,'b' : 200,'c' : 300, 'a' : 400} # if the key is duplicated, the dictionary can be updated with latest key:value pair d6 = {'a' : 100,'b' : 100,'c' : 100} print(type(d1)) print(type(d2)) print(type(d3)) print(type(d4)) print(d5) print(d6) # using dict() also we can create the dictionary d7 = dict({'apple' : 121,'banana' : 131,'cherry':144}) print(d7) # run time/dynamic dictionary # eval() and input() d8 = eval(input("Enter a dictionary:")) print(d8) =========================================================== # Dictionary Properties # Dictionary is ordered. d1 = {'a':111,'b':222,'c':333,'d':444} print(d1) # dictionary never be an indexed. # print(d1[0]) ==> key error # to access the values of the dictionary # we need to use "keys" # syntax: dictionary-name[key-name] print(d1['a']) print(d1['b']) print(d1['c']) print(d1['d']) # Keys of dictionary can be accessed using keys() # values of dictionary can be accessed using values() print(d1.keys()) print(d1.values()) for i in d1: # print(i) print(i,":",d1[i]) # items() ==> can return all the items of the dictionary print(d1.items()) for i in d1.items(): print(i) # dictionary is mutable. print(d1) print(id(d1)) # modification d1['c'] = 3333 print(d1) print(id(d1)) d1['e'] = 1010 print(d1) print(id(d1)) =================================================== # Dictionary aliasing and Cloning d1 = {'a':111,'b':222,'c':333,'d':444,'e':555} # aliasing d2 = d1 print(id(d1)) print(id(d2)) d1['p'] = 666 print(d1) print(d2) # cloning d3 = d1.copy() print(id(d1)) print(id(d3)) d3['r'] = 7979 print(d1) print(d3) ============================================ # update() # dictionary-name.update(x) # to extend the dictionary with another dictionary, we can use "extend()". d1 = {'a':111,'b':222,'c':333} d2 = {'d':444,'e':555,'f':666} d1.update(d2) print(d1) print(d2) ========================================================= # WAP TO ENTER NAME AND PERCENTAGE IN A DICTIONARY AND DISPLAY THE INFORMATION ON THE SCREEN. record = {} # student record ==> name and marks n = int(input("Enter number of students:")) i = 1 while i <= n: name = input("Enter the student name:") marks = input("Enter student marks:") record[name] = marks i += 1 print("Name of the student","\t","Marks of the student") for key in record: print("\t",key,"\t\t\t\t",record[key]) ========================================================== d1 = {'a' : 11,'b' : 22,'c' : 33,'d' : 44,'e' : 55} print(d1) # pop() ==> dict-name.pop(key) d1.pop('c') print(d1) # popitem() ==> to delete the last item from the dictionary d1.popitem() print(d1) d1.popitem() print(d1) # clear() ==> remove all the items from the dictionary d1.clear() print(d1) # del property ==> delete the dictionary permanently. del d1 print(d1) Assignment: =========== 1) WAP TO CREATE THE DYNAMICALLY DEFINED DICTIONARY AND FIND THE LENGTH OF THE DICTIONARY i) WITHOUT BUILT-IN METHOD ii) WITH BUILT-IN METHOD ==> len()