Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
sum() Function in Python
3.
Features of sum() Function in Python
4.
Applications of sum() Function in Python
5.
Various Operations using the sum() Function
5.1.
Average of Sequence of Numbers
5.2.
Cumulative Sum of a Sequence of Numbers
5.3.
Sum of the Squares of a Sequence of Numbers
5.4.
Sum of the Absolute Values of a Sequence of Numbers
5.5.
Sum of the Products of two Sequence of Numbers
6.
Arguments in sum() Function
7.
sum() Function in Python Examples
8.
Frequently Asked Questions
8.1.
How do I use the sum function with a list of strings?
8.2.
How do I calculate the sum of a dictionary in Python?
8.3.
What is TypeError in Python?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Learning sum() Function in Python

Introduction

Python is an easy-to-write general-purpose programming language for programmers. It is simple, has numerous applications, and is widely used in the Artificial Intelligence domain. Python provides us with multiple built-in functions, which greatly eases our efforts. We will discuss one of the built-in functions that we heavily rely on for our calculation, the sum function in Python. 

learning sum function in python

This article will discuss the sum function in Python. We will start our discussion with a quick intro to sum functions in Python. Afterwards, we will discuss its features, applications, operations, and its arguments. At last, we will see various examples of the sum functions in Python. So without further ado, let’s get started!

Recommended topics, Floor Division in Python, and Convert String to List Python

sum() Function in Python

The sum() function in Python is a built-in function provided by Python which helps us with our calculations. It is used to calculate the sum of a sequence of numbers. It takes an iterable as its first argument. It can be a list, tuple, or other object supporting iteration. The function returns the sum of all the elements in the iterable. It can also take an optional second argument, called start. It specifies the value that the sum should start with. By default, the start value is 0.

Parameter

Description

Iterable

It is an iterable object that supports iteration, such as a list, tuple, or any other object.

Start

This is an optional argument which is used to specify the starting value of the sum.

You should ensure that the iterable passed to the sum function in Python contains only numbers. The function will raise a TypeError exception if the iterable contains different data types.

The sum function in Python is commonly used for basic mathematical operations. For example, adding up a list of numbers, calculating the total of a series of values, or finding the mean of a list of numbers. It can also be used in combination with other operations. They can be list comprehension, lambda function, map and zip to perform more complex calculations on a sequence of numbers etc.

Also See, Python Round Function.

Features of sum() Function in Python

The sum function in Python is used to calculate the sum of a sequence of numbers. It particularly focuses on a list or a tuple.

Features of sum Function in Python

Some key features of the sum function in Python include:

  • The function takes an iterable as its first argument. It can be a list, tuple, or other object supporting iteration.
     
  • An optional second argument, called start, can be provided. It specifies the value that the sum should start with. By default, the start value is 0.
     
  • The sum function in Python can be used with numbers of any type, including integers, floating-point numbers, and complex numbers.
     
  • The function can also be used with any iterable that contains numbers. It can be a list of integers or a tuple of floating-point numbers.
     
  • The sum function returns the start value (default 0) if the iterable is empty.

Applications of sum() Function in Python

The sum function in Python is primarily used to calculate the sum of a sequence of numbers. Still, it can also be used in a variety of other applications. 

Applications of sum Function in Python

Some examples of the sum function use include:

  • For summing up the values in a list or tuple of numbers. Such as the elements of a matrix or a list of financial transactions.
     
  • For calculating the total of a series of values. For example, the total cost of a shopping cart or the total distance travelled in a trip.
     
  • For summing up the values in a specific column or row of a matrix or data frame using list comprehension.
     
  • For checking the validity of a set of data by comparing the calculated sum to a known value. For example, a checksum or a hash value.
     
  • For finding the mean of a list of numbers. It can be done by dividing the sum of the numbers by the length of the list.

Various Operations using the sum() Function

The sum function in Python can be used in combination with other operations to perform a variety of calculations on a sequence of numbers. Some operations that can be performed using the sum function include:

Average of Sequence of Numbers

Finding the average of a sequence of numbers.

numbers = [1, 2, 3, 4, 5]
average = sum(numbers) / len(numbers)
print(average)
# Output: 3.0

Cumulative Sum of a Sequence of Numbers

Calculating the cumulative sum of a sequence of numbers.

numbers = [1, 2, 3, 4, 5]
cumulative_sum = [sum(numbers[:i+1]) for i in range(len(numbers))]
print(cumulative_sum)
# Output: [1, 3, 6, 10, 15]

Sum of the Squares of a Sequence of Numbers

Finding the sum of the squares of a sequence of numbers.

numbers = [1, 2, 3, 4, 5]
sum_of_squares = sum(x ** 2 for x in numbers)
print(sum_of_squares)
# Output: 55

Sum of the Absolute Values of a Sequence of Numbers

Finding the sum of the absolute values of a sequence of numbers.

numbers = [-1, -2, 3, 4, -5]
sum_of_absolute_values = sum(abs(x) for x in numbers)
print(sum_of_absolute_values)
# Output: 15

Sum of the Products of two Sequence of Numbers

Finding the sum of the products of two sequences of numbers.

numbers1 = [1, 2, 3, 4, 5]
numbers2 = [2, 3, 4, 5, 6]
sum_of_products = sum(x * y for x, y in zip(numbers1, numbers2))
print(sum_of_products)
# Output: 70

These are just a few examples of the types of calculations that can be performed using the sum function in Python. The function can be used in combination with other operations such as list comprehension, lambda function, map and zip to perform more complex calculations on a sequence of numbers.

Arguments in sum() Function

The sum function in Python takes two arguments:

  1. Iterable: This is the first and mandatory argument of the function. It is an iterable object that supports iteration, such as a list, tuple, or any other object. The function will sum up all the elements in this iterable.
     
  2. Start: This is an optional argument which is used to specify the starting value of the sum. By default, the start value is 0. This can be any number, including integers, floating-point numbers, and complex numbers.

Example 1:

numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print(result)
# Output: 15

In the example, the iterable argument is a numbers list, and the start argument is not provided, so it will take the default value 0.

 

Example 2:

numbers = [1, 2, 3, 4, 5]
result = sum(numbers,10)
print(result)
# Output: 25

The iterable argument is a numbers list in the example, and the start argument is 10. So it will add 10 to the list's total elements and return 25.

You can also know about Python Square Root here.

sum() Function in Python Examples

The sum function in Python is used to calculate the sum of a sequence of numbers, typically a list or a tuple. Here are some examples of using the sum function in Python:

Example 1: Summing up the values in a list of numbers:

numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print(result)
# Output: 15

 

Example 2: Summing up the values in a tuple of numbers:

numbers = (1, 2, 3, 4, 5)
result = sum(numbers)
print(result)
# Output: 15

 

Example 3: Summing up the values in a specific column of a matrix:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
column = 1
result = sum(matrix[i][column] for i in range(len(matrix)))
print(result)
# Output: 12

 

Example 4: Using the start argument to specify a starting value for the sum:

numbers = [1, 2, 3, 4, 5]
result = sum(numbers, 10)
print(result)
# Output: 25

 

Example 5: Using the sum function with a generator expression:

numbers = (x*x for x in range(1,6))
result = sum(numbers)
print(result)
# Output: 55

 

As you can see from these examples, the sum function in Python can be used with lists, tuples, generator expressions, and dictionary values. The function can be used in combination with other operations such as list comprehension, lambda function, map and zip to perform more complex calculations on a sequence of numbers.

Must Read Python List Operations

Frequently Asked Questions

How do I use the sum function with a list of strings?

The sum function can only be used with numbers. If you have a list of strings and you want to sum them, you can first convert them to numbers using the int() or float() functions, then use the sum function.

How do I calculate the sum of a dictionary in Python?

To calculate the sum of a dictionary in Python, you can use the sum function in combination with the values() method of the dictionary. This will return a list of the values of the dictionary, which can then be passed to the sum function.

What is TypeError in Python?

A TypeError in Python is an error that occurs when a function or operation is applied to an object of an incorrect type. For example, if you try to add two strings together using the + operator, Python will raise a TypeError because that operator is used for numerical addition, not string concatenation. 

Conclusion

This article briefly discussed the sum function in Python. We learnt that the sum function in Python is a built-in function that returns the sum of all the elements in that iterable. We discussed its features, applications, operations, arguments and examples. We hope this blog has helped you learn about merge sort in Python. If you like to learn more, you can check out our articles: 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive Programming and many more! If you wish to test your competency in coding, check out the mock test series and take part in the contests hosted on Coding Ninjas Studio! 

If you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

Nevertheless, consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass