DICTIONARY OPERATIONS: ====================== GETTING THE VALUES OF THE DICTIONARY: ==================================== get() ==== ==> an inbuilt method which we can use to get the value of the dictionary according to the specified key. Syntax: dictionary-name.get(key-name) # get() method record = {'Amisha' : "99%", "Hyma" : "99%", "Gazala" : '98%'} # getting the value which is associated to the specified key val1 = record.get('Hyma') val2 = record.get("Amisha") val3 = record.get("Gazala") print(val1) print(val2) print(val3) # if the specified key is not in the given dictionary, get() can return "None" val4 = record.get('Ravi') print(val4) ================================================ values() ======= ==> is an inbuilt method which we can use to get all the values of the dictionary. Note: ===== values() cannot required any key as reference for returning the values. Syntax: list_of_values = dictionary-name.values() # values() record = {'Apple' : "100Kg","Cherry" : "25kg","Mango" : "100Kg","Kiwi" : "50Kg"} list_values = record.values() print(list_values) # to access the values of the dictionary, we can do traversing for i in record.values(): print(i) ======================================================== Getting of the keys from the dictionary: ======================================== keys(): ======= is an inbuilt method which we can use to get all the keys of the dictionary in a list format. Syntax: list_keys = dictionary-name.keys() # keys() record = {'a' : 111, 'b' : 222, 'c' : 333, 'd' : 444, 'e' : 555} list_keys = record.keys() print(list_keys) # Accessing of keys of dictionary individually for i in record.keys(): print(i) =================================================== Getting of Items of the dictionary in an arbitrary format: ========================================================== (key, value) ==> arbitrary data items(): ======= is an inbuilt method which we can use to get all the items in arbitrary format from the dictionary. Syntax: list-items = dictionary-name.items() # items() record = {'a' : 1000, 'e' : 2000, 'i' : 3000, 'o' : 4000, 'u' : 5000} list_items = record.items() print(list_items) for i in record.items(): print(i) ================================================ Aliasing To Dictionary: ====================== # Dictionary Aliasing d1 = {'a':11,'b':22,'c':33,'d':44,'e':55} d2 = d1 # alias print("Before the modification:") print("Address of Dictionaries are:") print(id(d1)) print(id(d2)) print("The Given Dictionaries are:") print(d1) print(d2) d2['e'] = 1221 print("After the modification:") print("Address of Dictionaries are:") print(id(d1)) print(id(d2)) print("The Given Dictionaries are:") print(d1) print(d2) Cloning of the Dictionary: ========================== copy() ====== is an inbuilt method which we can create the new dictionary from another dictionary. Syntax: new-dict = old-dict.copy() # dictionary cloning using copy() d1 = {'a' : 101, 'e' : 11001, "i" : 121,'o':1221,'u' : 12221} d2 = d1.copy() print("Before the change on the dictionaries:") print("The Addresses are:") print(id(d1)) print(id(d2)) print("The Dictionaries are:") print(d1) print(d2) d1['e'] = 97 print("After the change:") print("The Addresses are:") print(id(d1)) print(id(d2)) print("The Dictionaries are:") print(d1) print(d2) ================================================ setdefault(): ============= Syntax: dict-name.setdefault(key,value) # setdefault() d1 = {} print(d1) d1.setdefault('abc',1221) d1.setdefault('pqr',9779) d1.setdefault('xyz',7997) print(d1) # if the specified keys is already in the dictionary d1.setdefault('pqr',12345) print(d1) =================================== update(): ======== to add one dictionary data to another dictionary, we can use, update() Syntax: dict1.update(dict2) # update() d1 = {'a':1,'e':5,'i':25,'o':125,'u':625} d2 = {'p':111,'q':222,'r':333} print("The Given Dictionaries are:") print(d1) print(d2) d1.update(d2) print("Dictionaries after change are:") print(d1) ===================================== # WAP IN PYTHON TO FIND NUMBER OF OCCURRENCES OF EACH VOWEL PRESENT IN THE GIVEN STRING """ str = "Object-Oriented Programming Language" vowels = ['a','e','i','o','u'} Op: 'O' ==> 2 'e' ==> 4 """ data = input("Enter a string:") vowels = {'a','A','e','E','i','I','o','O','u','U'} record = dict() for i in data: if i in vowels: record[i] = record.get(i,0)+1 for j,k in sorted(record.items()): print(j,"Occurred",k,'times') print(d2)