Class 12: Passing dictionary to the function

Example 1:
Python script to pass a dictionary to the function and access its elements.
Solution:

#fucntion definition
def dict(d):
    for key in d:
        print(key,":",d[key])

d={'m':'maths','s':'science','c':'commerce','e':'english','h':'hindi'}
dict(d)
#Output:
m : maths
s : science
c : commerce
e : english
h : hindi
>>> 

Example 2:
Python script to pass a dictionary to the function and add element to it.
Solution:

#fucntion definition
def dict(d):
    d['Ravi']=20
    print(d)

dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
dict(dic1)
# Output:
{'Teena': 18, 'Riya': 12, 'Aliya': 13, 'Priya': 17, 'Ravi': 20}
>>>

Example 3:
Python script to pass two dictionaries to the function and add elements of one dictionary to another.
Solution:

#fucntion definition
def dict(d1,d2):
    print(d1)
    print(d2)
    d1.update(d2)
    print(d1)
    
dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
dic2={'amit':20,'Ali':22,'Riya':20}
dict(dic1,dic2)
# Output:
{'Teena': 18, 'Riya': 12, 'Aliya': 13, 'Priya': 17}
{'amit': 20, 'Ali': 22, 'Riya': 20}
{'Teena': 18, 'Riya': 20, 'Aliya': 13, 'Priya': 17, 'amit': 20, 'Ali': 22}
>>> 

Example 4:
Python script to pass a dictionary to the function and delete an element.
Solution:

#fucntion definition
def dict(d1):
    print(d1)
    e=input('enter the element to be deleted: ')
    d1.pop(e)
    print(d1)
    
dic1={'Teena':18,'Riya':12,'Aliya':13,'Priya':17}
dict(dic1)
# Output:
{'Teena': 18, 'Riya': 12, 'Aliya': 13, 'Priya': 17}
enter the element to be deleted: Riya
{'Teena': 18, 'Aliya': 13, 'Priya': 17}
>>>