DICTIONARY OPERATIONS: ====================== TRAVERSING ON DICTIONARY DATA: ============================== WHILE FOR # WAP TO ENTER NAME AND PERCENTAGE OF MARKS IN A DICTIONARY AND DISPLAY THE INFORMATION ON THE SCREEN """ record = {"stu1":77%,"stu2":78%,"stu3":79%} 1) create the empty dictionary. 2) add items into the dictionary. item ==> pair of name and percentage marks 3) display the dictionary information """ # record = {} record = dict() n = int(input("Enter the count for the record:")) i = 1 while i <= n: name = input("Enter the name of the student:") percentage = input("Enter percentage marks of the student:") record[name] = percentage i += 1 print("The Student Data in the given record is:") for item in record: print("\t",item,"\t\t\t",record[item]) ========================== How to modify the dictionary data? ================================== """ for the modification: 1) create the dictionary 2) type of definition 3) add the item into the dictionary dict-name['new-key'] = value 4) modification on the dictionary dict-name['mod_key'] = new-value """ fruits = eval(input("Enter the record of Fruits:")) print(type(fruits)) print(id(fruits)) # adding items into dictionary fruits['Kiwi'] = 250 fruits['JackFruit'] = 125 # modification of items in dictionary fruits['Banana'] = 500 fruits['Cherry'] = 1000 # printing of dictionary print(fruits) print(id(fruits)) ========================================== Delete Operations on Dictionary: ================================ 1) del property: ================ ==> using del property, we can delete the specific item from the dictionary Syntax: del dict-name[key] ==> we can delete the whole dictionary Syntax: del dict-name # delete operations record = dict({'Amisha':'77%','Hyma':'78%','Gazala':'79%','abc':'90%'}) rec = dict({('Amisha','87%'),('Gazala','88%'),('Hyma','89%')}) print(type(rec)) print(type(record)) print(record) print(rec) # deletion of specific item from dictionary del record['abc'] print(record) # deletion of whole record del rec # print(rec) ============================================ 2) clear(): ========== ==> can use to clear the whole data from the dictionary. ==> return an empty dictionary. Syntax: dict-name.clear() record = dict({'Amisha':'77%','Hyma':'78%','Gazala':'79%','abc':'90%'}) rec = dict({('Amisha','87%'),('Gazala','88%'),('Hyma','89%')}) print(record) print(rec) record.clear() rec.clear() print(record) print(rec) ==================================================== 3) pop(): ======== ==> which can use to delete the value of the dictionary which is corresponding to the specified key Syntax: dict-name.pop(key) ==> returns: value, which is deleting ==> if the specified item is not in the given dictionary: "key Error" ==> if the pop() can define without specifying the key. "type Error". record = dict({'Amisha':'77%','Hyma':'78%','Gazala':'79%','abc':'90%'}) print(record) ret = record.pop('abc') print(ret) print(record) # record.pop('abc') # record.pop() ================================= 4) popitem() ============ ==> delete the arbitrary items (pair of key and value) of the dictionary from the last by default. ==> return: that pair which is deleting. Syntax: dict-name.popitem() record = dict({'Amisha':'77%','Hyma':'78%','Gazala':'79%','abc':'90%'}) print(record) data = record.popitem() print(data) print(record) ========================================= Finding length of the dictionary: ================================= len() ===== can use to find the length of any collection. Syntax: len(dictionary-name) # finding length dictionary = eval(input("Enter a dictionary:")) length = len(dictionary) print("Length of the dictionary is = ",length) ============================== Assignment: ========== WAP IN PYTHON TO FIND THE LENGTH OF ANY COLLECTION (Dictionary) WITHOUT USING ANY PRE-DEFINED METHOD.