DICTIONARY DATA OPERATIONS ========================== LIST, TUPLE, SET ==> COLLECTION WITH VALUES [1,2,3,4] ==> LIST (1,3,5,7,9) ==> TUPLE {1,2,3,4,5} ==> SET APPLICATION FORM: ================= ZOOM REGISTRATION NAME : RAVI MAIL : RAVIVRAOINFS@GMAIL.COM PHONE : 89XXXXXXX PASSWORD : XXXXXXXX SIGNUP ZOOM SERVER ZOOM LOGIN: USERNAME : PASSWORD : SIGNIN COMPUTER ===> SERVER KEY AND VALUE PAIR FORMAT WHAT IS DICTIONARY? ================== 1) COLLECTION DATATYPE COLLECTION OF ITEMS HERE: ITEM ==> KEY AND VALUE PAIR Syntax: key : value Syntax: dictionary-name = {key1:value1,key2:value2,key3:value3} Ex: record = {'name' :'ravi','gender' : 'male','phone' : 8977029779,'mail' : 'abc@gmail.com'} Properties of Dictionary Data: ============================== 1) {} 2) Inbuilt Datatype an inbuilt class dict type() 3) Dictionary is a collection of key and value pairs 4) Keys in Dictionary ==> with any datatype 5) Values in dictionary ==> with any datatype Note: ==== Keys always considered to define with primitive datatypes only values can possible to define with primitive datatypes and collections also. 6) Homogeneous elements 7) Heterogeneous elements keys ==> same datatype and values ==> same datatype ==> Homogeneous ex: 'str1' : 10, 'str2' : 20 keys ==> different datatype and values ==> different datatype ==> Heterogeneous ex: 12 : 'str1', 'str2: 12.23 8) Ordered datatype 9) Keys ==> Unique/not duplicated 10) Values ==> Duplicated 11) Mutable datatype 12) Not Index Supported to access the individual values from the dictionary, we can use "keys" Syntax: dict-name[key] Note: ==== if index value is as a key in a dictionary, the value of dictionary can be accessed if index value is as not a key ==> Key Error. # Properties of Dictionary d1 = {} # empty dictionary d2 = set() # empty set d3 = {11 : 'a','b' : 21, 1-2j : True} # Heterogeneous # d4 = {[1,2,3] : (1,2,3)} d4 = {'list' : [1,2,3,4],'tuple' : (1,3,5,7,9),'set' :{11,13,15,17,19}} d5 = {'a' : 11,'b' : 22, 'c':33} # Homogeneous d6 = {'abc' : 111,'def' : 222, 'ghi' : 333, 'abc' : 555} d7 = {'a' : 10,'b' : 20,'c' : 30,'d':30,'e':20,0:245} print(type(d1));print(type(d2));print(type(d3));print(type(d4));print(type(d5)) print(d1);print(d2);print(d3);print(d4);print(d5);print(d6);print(d7) print(d3['b']);print(d4['set']);print(d5['a']);print(d6['abc']) print(d7[0]) # print(d7[1]) print(id(d7)) print(d7) # adding of element into dictionary, we can use key and value d7['pqr'] = {1,3,5,7,9} d7['a'] = 100 print(id(d7)) print(d7) ============================================================== Traversing On Dictionary Data: ============================== # WAP TO ENTER NAME AND PERCENTAGE OF MARKS IN A DICTIONARY AND DISPLAY THAT INFORMATION. stu_record = dict() # empty dictionary size = int(input("Enter the size of the record:")) print("Enter The student data:") i = 1 while i <= size: name = input("Enter the name of the student:") percentage = input("Enter the percentage of the student:") stu_record[name] = percentage i = i + 1 print("Student's Name","\t","Student's Percentage") for stu in stu_record: print('\t',stu,'\t\t\t',stu_record[stu]) ============================================== Delete the data from Dictionary: =============================== 1) del property =============== ==> using 'key', we can delete any data from the dictionary Syntax: del dict-name[key] ==> we can delete whole dictionary data permanently. Syntax: del dict-name Note: if the specified key is not in the dictionary ==> Key error. d1 = eval(input("Enter a dictionary:")) print("The Dictionary is = ",d1) del d1[33] print("The Dictionary is = ",d1) del d1 print(d1) =========================================================== 2) clear() ========== ==> to clear the whole dictionary, we can use clear() Syntax: dict-name.clear() d1 = eval(input("Enter a dictionary:")) print("The Dictionary is = ",d1) d1.clear() print("The Dictionary is = ",d1) ========================================================= 3) pop() ========= we can use to delete the data based on the key. Syntax: dict-name.pop(key) d1 = eval(input("Enter a dictionary:")) print("The Dictionary is = ",d1) d1.pop('d') print("The Dictionary is = ",d1) ======================================================== 4) popitem() =========== ==> we can use to delete the data in arbitrary format (key and value) from the end of the dictionary. Syntax: dict-name.popitem() d1 = eval(input("Enter a dictionary:")) print("The Dictionary is = ",d1) d1.popitem() print("The Dictionary is = ",d1)