Python sum() Function with Examples

sum() Function in Python:

The sum() function returns a number that represents the sum of all items in an iterable.

Syntax:

sum(iterable, start)

Parameters

iterable: This is required. It is the sequence  to be sumed.

start: This is optional. It is a value added to the return value. The value of start is 0 by default (if omitted)

Return Value:

sum() returns the sum of the iterable’s start and items.

Examples:

Example1:

Input:

Given List = [1, 2, 6, 15, 5]

Output:

The sum of all the items in a given list = 29

Example2:

Input:

Given List = [1, 2, 3, 5, 10]
start value = 4

Output:

The sum of all the items in a given list with given start value =  25

sum() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)Without start value

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the sum() function to get the sum of all the items in a given list.
  • Store it in another variable.
  • Print the sum of all the items in a given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 6, 15, 5]
# Pass the given list as an argument to the sum() function to get the sum of all
# the items in a given list
# Store it in another variable.
summ_lst = sum(gvn_lst)
# Print the sum of all the items in a given list.
print("The sum of all the items in a given list =", summ_lst)

Output:

The sum of all the items in a given list = 29
2)With start value

Approach:

  • Give the list as static input and store it in a variable.
  • Give the start value as static input and store it in a variable.
  • Pass the given list, start value as the arguments to the sum() function to get the sum of all the items in a given list. Begin with the given start value and add all of the items in a list to it.
  • Store it in another variable.
  • Print the sum of all the items in a given list with the given start value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 3, 5, 10]
# Give the start value as static input and store it in a variable.
gvn_strt = 4
# Pass the given list, start value as the arguments to the sum() function to
# get the sum of all the items in a given list.
# Begin with the given start value and add all of the items in a list to it.
# Store it in another variable.
summ_lst = sum(gvn_lst, gvn_strt)
# Print the sum of all the items in a given list with the given start value.
print("The sum of all the items in a given list with given start value = ", summ_lst)

Output:

The sum of all the items in a given list with given start value =  25

Method #2: Using Built-in Functions (User Input)

1)Without start value

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the sum() function to get the sum of all the items in a given list.
  • Store it in another variable.
  • Print the sum of all the items in a given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the sum() function to get the sum of all
# the items in a given list
# Store it in another variable.
summ_lst = sum(gvn_lst)
# Print the sum of all the items in a given list.
print("The sum of all the items in a given list =", summ_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40 10
The sum of all the items in a given list = 110
2)With start value

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the start value as user input using the int(input()) function and store it in a variable.
  • Pass the given list, start value as the arguments to the sum() function to get the sum of all the items in a given list. Begin with the given start value and add all of the items in a list to it.
  • Store it in another variable.
  • Print the sum of all the items in a given list with the given start value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the start value as user input using the int(input()) function
# and store it in a variable.
gvn_strt = int(input("Enter some random number = "))
# Pass the given list, start value as the arguments to the sum() function to
# get the sum of all the items in a given list.
# Begin with the given start value and add all of the items in a list to it.
# Store it in another variable.
summ_lst = sum(gvn_lst, gvn_strt)
# Print the sum of all the items in a given list with the given start value.
print("The sum of all the items in a given list with given start value = ", summ_lst)

Output:

Enter some random List Elements separated by spaces = 10 20 30 40 10
Enter some random number = 20
The sum of all the items in a given list with given start value = 130