In this python function program, we will create a function to get the square of all values in the given dictionary.
What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.
Steps to solve the program
- Create a function square.
- Use the def keyword to define the function.
- Pass a parameter i.e. dictionary to the function.
- Create an empty dictionary.
- Use a for loop to iterate over the keys and values of the dictionary.
- Add the key the empty dictionary and sqaure of the original value as the new value.
- Return the new dictionary.
- Pass the dictionary to the function while calling the function.
def square(d):
a = {}
for key,value in d.items():
a[key] = value**2
return a
square({'a':4,'b':3,'c':12,'d':6})
Output :
{'a': 16, 'b': 9, 'c': 144, 'd': 36}
Related Articles
Python function program to create dictionary output from the given string.
Python function program to print a list of prime numbers from 1 to 100.
Python function program to get a list of odd numbers from 1 to 100.
Python function program to print and accept login credentials.
Python function program to get the addition with the return statement.
Python function program to create a Fruitshop Management system.