python program to sum all items of a dictionary
Answers
Answer:
Python program to find the sum of all items in a dictionary
Approach #1 : Using Inbuilt sum() Function. Use sum function to find the sum of dictionary values. ...
Approach #2 : Using For loop to iterate through values using values() function. ...
Approach #3 : Using For loop to iterate through items of Dictionary.
Sum of the dictionary
Output:
Sum : 1122
Explanation:
#define the function
def returnS(dt):
#declare the variable to 0
add = 0
#set the for loop
for i in dt:
#add the values of the dictionary
add = add + dt[i]
return add
#declare the dictionary type variable
dt = {'a': 456, 'b':123, 'c':543}
#call and print the function
print("Sum :", returnS(dt))
The following are the description of the program.
- Declare the function 'returnS()' and pass the argument 'dt' in the parameter of the function.
- Declare the variable 'add' and initialize the value to 0.
- Then, set the for loop that perform the calculation with the values of the dictionary.
- Finally, declare the dictionary type variable 'dt' and initialize the values in it and call the function by passing the following variable as argument.
Learn More:
https://brainly.in/question/11805680