Python function program to get the square of all values in the given dictionary

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
  1. Create a function square.
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. dictionary to the function.
  4. Create an empty dictionary.
  5. Use a for loop to iterate over the keys and values of the dictionary.
  6. Add the key the empty dictionary and sqaure of the original value as the new value.
  7. Return the new dictionary.
  8. 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

get the duplicate characters from the string.

create dictionary output from the given string.

Leave a Comment