 ### sum() Function in Python - Step by step guide ?

Python Tutorials

#### What is the sum() Function in Python ?

The sum() function prints out the sum of the values ​​of the list. Basically, the sum() function can be used to calculate the sum of all values in an iterable object. This method is useful when you need the total value of a list of items, which is common in a number of mathematical calculations.

#### Python sum() Function Syntax

The Python sum() function adds up all the numerical values in an iterable, such as a list, and returns the total of those values. sum() calculates the total of both floating-point numbers and integers.

For instance, you could use the sum() method to calculate the total price of the products a customer purchases at a store.

Here's the syntax for the Python sum() method:

`\$ sum(iterable_object, start_value)`

The function sum() takes in two parameters:

• The iterable object that you would like to calculate the total of (required) such as lists, tuples.
• An extra number that you want to add to the value you're calculating (optional) which you want to add to the result.

#### sum() Return Value

sum() returns the sum of start and items of the given iterable.

#### Examples of using sum() Function

1. Take a look at the below function:

`a = (1, 2, 3)x = sum(a)print(x)`

It's output will give:

`6`

2. The basic sum() function:

`a = (3, 4, 5)# Apply sum() functionx = sum(a)# Return resultprint("Sum =",x)`

It's Output will give:

`Sum = 12`

3. Increase results:

`a = (3, 4, 5)# Apply sum() functionx = sum(a)print(x)# Plus 10y = sum(a, 10)print(y)`

Output will give:

`1222`

Conclusion

The Python sum() function adds up all the numerical values in an iterable, such as a list, and returns the total of those values.
sum() basically calculates the total of both floating-point numbers and integers.

Explore:
, , , , , , ,