Everywhere, the total of the numbers in the list is necessary. The built-in Python function sum() adds up all of the values in the list.
Python’s built-in sum() function is a flexible function that lets you figure out how much each member in an iterable—like lists, tuples, or other sequences—is worth. It offers a practical means of rapidly adding values within a collection without requiring explicit loops. We will examine the many parameters and features of the sum() function in detail in this lesson, along with providing clear examples to help you understand.
Sum() Function in Python Syntax:
Syntax : sum(iterable, start)
- Iterable: This can be any list, tuple, dictionary, but ideally it should be composed of integers.
- start: the start is added to the iterable’s total number of values. It is presumed that start is 0 if it is not specified in the syntax.
Python Sum() Function Examples:
numbers = [1,2,3,4,5,1,4,5] Sum = sum(numbers) print(Sum) Sum = sum(numbers, 10) print(Sum)
Output: 25 35
Here below we cover some examples using the sum function with different datatypes in Python to calculate the sum of the data in the given input
- Sum Function on a Dictionary
- Sum Function on a Set
- Sum Function on a Tuple
- The sum in Python with For Loop
- Error and Exceptions
- Practical Application
Python Sum Function on a Dictionary :
In this example, we are creating a tuple of 5 numbers and using sum() on the dictionary in Python.
my_dict = {'a': 10, 'b': 20, 'c': 30} total = sum(my_dict.values()) print(total)
Output: 60 Time complexity: O(1) Space complexity: O(n)
Python Sum Function on a Set:
In this example, we are creating a tuple of 5 numbers and using sum() on the set in Python.
my_set = {1, 2, 3, 4, 5} total = sum(my_set) print(total)
Output: 15
Python Sum Function on a Tuple:
n this example, we are creating a tuple of 5 numbers and using sum() on the tuple in Python.
my_tuple = (1, 2, 3, 4, 5) total = sum(my_tuple) print(total)
Output: 15 Time complexity: O(1) Space complexity: O(n)
The sum in Python with For Loop:
In this, the code first defines a list of numbers. It then initializes a variable called total to 0. The code then iterates through the list using a for loop, and for each number in the list, it adds that number to the total variable. Finally, the code prints the total value, which is the sum of the numbers in the list.
# Define a list of numbers numbers = [10, 20, 30, 40, 50] # Initialize a variable to store the sum total = 0 # Iterate through the list and add each number to the total for num in numbers: total += num # Print the sum of the numbers print("The sum of the numbers is:", total)
Output: The sum of the numbers is: 150 Time complexity: O(n) Space complexity: O(n)
Error and Exceptions:
TypeError: If the list contains any characters other than numbers, this error is raised. Instead of utilizing an integer in the example provided, we are using a list of strings.
arr = ["a"] # start parameter is not provided Sum = sum(arr) print(Sum) # start = 10 Sum = sum(arr, 10) print(Sum)
Output: Traceback (most recent call last): File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in Sum = sum(arr) TypeError: unsupported operand type(s) for +: 'int' and 'str'
Practical Application :
Issues where computing the total is necessary to perform other tasks, like determining the average of a set of numbers.
numbers = [1,2,3,4,5,1,4,5] # start = 10 Sum = sum(numbers) average= Sum/len(numbers) print (average)
Output: 3