Sunday, 19 January 2025

Dy 93: Python Program to Find the Sum of All the Items in a Dictionary


 def sum_of_values(dictionary):
    """
    Calculate the sum of all the values in a dictionary.

    Args:
        dictionary (dict): The dictionary containing numerical values.

    Returns:
        int or float: The sum of all the values.
    """
    return sum(dictionary.values())

my_dict = {"a": 10, "b": 20, "c": 30}

total_sum = sum_of_values(my_dict)

print(f"The sum of all values in the dictionary is: {total_sum}")

#source code --> clcoding.com 


Code Explanation:

def sum_of_values(dictionary):
This line defines a function named sum_of_values that takes one parameter called dictionary. This function is designed to calculate the sum of all the values in the given dictionary.

"""
A docstring (comment between triple double quotes) is used to explain the purpose of the function. It mentions:
The purpose of the function: "Calculate the sum of all the values in a dictionary."
The parameter (dictionary), which is expected to be a dictionary with numerical values.
The return value, which will either be an integer or a float (depending on the type of values in the dictionary).

return sum(dictionary.values())
The function uses the built-in sum() function to calculate the sum of all values in the dictionary.
dictionary.values() extracts all the values from the dictionary (e.g., [10, 20, 30] for {"a": 10, "b": 20, "c": 30}).
The sum() function adds these values together and returns the total (in this case, 10 + 20 + 30 = 60).

my_dict = {"a": 10, "b": 20, "c": 30}
This line creates a dictionary named my_dict with three key-value pairs:
Key "a" has a value of 10.
Key "b" has a value of 20.
Key "c" has a value of 30.

total_sum = sum_of_values(my_dict)
The sum_of_values function is called with my_dict as its argument.
Inside the function, the values [10, 20, 30] are summed up to give 60.
The result (60) is stored in the variable total_sum.

print(f"The sum of all values in the dictionary is: {total_sum}")
The print() function is used to display the result in a formatted string.
The f-string allows the value of total_sum (which is 60) to be directly inserted into the string.

Output:
The sum of all values in the dictionary is: 60

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (90) AI (37) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (184) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (135) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (19) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (5) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (959) Python Coding Challenge (402) Python Quiz (56) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses