Scripting

Python sum() Function

Python sum() Function

Introduction

The sum() function in Python is used to sum the numbers in a list. Summing a list of numeric values ​​is a useful and mandatory thing everywhere. So for a Python programmer, sum() is a useful tool. You can match lists and tuples together by using sum().

And we will guide you on how to use the sum() function of Python as we did below. Hope you understand.

Example

a = (1, 2, 3)

x = sum(a)

print(x)

Output:

6

Definition

The sum() function prints out the sum of the values ​​of the list.

The syntax

sum(iterable, start)

Parameter Values:

iterable: lists, tuples, … (Required)

start: the value you want to add to the result (Optional)

More examples

Example 1: The basic sum() function

a = (3, 4, 5)

# Apply sum() function

x = sum(a)

# Return result

print("Sum =",x)

Output:

Sum = 12

Example 2: Increase results

a = (3, 4, 5)

# Apply sum() function

x = sum(a)

print(x)

# Plus 10

y = sum(a, 10)

print(y)

Output:

12

22

Conclusion

We have guided you on how to use the sum() function in Python.

Thanks for referring!

Similar Posts