Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Sum Function in Python

Last Updated on April 27, 2023 by Prepbytes

The sum Function in Python is a quick and simple way to calculate the sum of a large set of numbers without having to write out a long loop or complicated mathematical formula. In this article, we will explore the basics of using the sum function in Python, including its syntax, parameters, and examples of how to apply it in different scenarios.

What is Sum Function?

The sum function in Python is a powerful tool that is used for adding up the values that are present in an iterable object such as a list or a tuple. The sum function in Python can take a single iterable argument or two arguments if the second argument is used to provide a start value.

Syntax of Sum Function in Python

The syntax of the sum function in Python is relatively simple, and it is as follows:

sum(iterable[, start])

Here in the above syntax,

  • The "iterable" is the iterable object (such as a list, tuple, or set) for which we want to calculate the sum.
  • The "start" argument is an optional argument and is used to provide a start value for the sum. If the start argument is not provided, the default value of 0 is used.

Return Value of Sum Function in Python

The sum function in Python returns the sum of the values in the given iterable object. If the iterable contains integers, the result will be an integer, and if the iterable contains floating-point numbers, the result will be a floating-point number.

Time Complexity of Sum Function in Python

The time complexity of the sum function in Python is O(n) in the average case scenario, where "n" is the number of elements in the iterable object. This means that the time taken to calculate the sum increases linearly with the number of elements in the iterable.

Examples of Sum Function in Python

Now that we have acquired a basic knowledge of the syntax of the sum function in Python, let’s look at some examples to illustrate how it works.

Example 1 of Sum Function in Python: Sum of a List of Integers
The sum function in Python is used to calculate the sum of all integers present in a list. This has been implemented in the below code.

Code Implementation:

numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
# Print the result
print(result)

Output

15

Explanation:
In this example, we define a list of integers and then use the sum function in Python to calculate the sum of the values in the list. The result is then printed to the console.

Example 2 of Sum Function in Python: Sum of a Tuple of Floating-Point Numbers
The code shows how we can calculate the sum of all floating numbers present in the list using the sum function in Python.

Code Implementation:

values = (3.14, 1.414, 2.718)
result = sum(values)
# Print the result
print(result)

Output

7.272

Explanation:
In this example, we define a tuple of floating-point numbers and then use the sum function in Python to calculate the sum of the values in the tuple. The result is then printed to the console.

Example 3 of Sum Function in Python: Summing a Subset of a List
We can use the sum Function in Python to find the sum of a subset of a list. The following code accomplishes this by using the slicing operator.

Code Implementation:

numbers = [1, 2, 3, 4, 5]
total = sum(numbers[:3])
print(total) 

Output

6

Explanation:
In this example, we have to sum only a subset of a list, we can use a slice. Here we make a variable name ‘total’, here we use slicing to get a list of the first three elements of a given list and if we observe on the output screen we get which is actually the sum of the first three elements of the list

Example 4 of Sum Function in Python: Summing a List of Tuples
We can also use the sum function in Python to calculate the sum of a list of tuples with the help of a for loop.

Code Implementation:

tuples = [(1, 2), (3, 4), (5, 6)]
total = sum(x[0] for x in tuples)
print(total)

Output

9

Explanation
In the above example, we have used the sum function in Python to sum the elements of a list of tuples. Here we simply iterate over on the tuple element and we simply pick the first element of the index and use the sum function to add up all the first elements of the tuple.

Example 5 of Sum Function in Python: Summing a List of Strings
Here is the code for summing up the list of strings

Code Implementation:

strings = ["1", "2", "3", "4", "5"]
total = sum(int(x) for x in strings)
print(total) 

Output

15

Explanation
In the above example, the list consists of strings that represent numbers in either integer or floating-point format. To calculate their sum, we can convert these strings to the respective data type and then use the sum function in Python to add all the elements of the list together. This involves converting the strings to integers before passing them to the sum function in Python, which allows us to easily compute their sum.

Example 6 of Sum Function in Python: Summing a List of Boolean Values
Here is another example showing the usage of the sum function in Python.

Code Implementation:

bools = [True, False, True, True, False]
total = sum(bools)
print(total)

Output:

3

Explanation
In the above example, we have a list name bools which has bool values, after this, we have to make the variable ‘total’ and use the sum function, here we got the 3 on output or console which means, the total true present is 3

Example 7 of Sum Function in Python: Summing a List of Objects with a Numeric Property
A list of objects that have a numeric property can also be summed by using the sum function in Python.

Code Implementation:

orders=[{"total": 10.99},{"total": 5.99},{"total": 7.99}]
total = sum(order["total"] for order in orders)
print(total) 

Output:

24.97

Explanation
If you have a list of objects that have a numeric property, you can use the sum function to sum the values of that property. Here we have a list of dictionaries representing sales orders with a total key and we use the sum function to sum the values of that property.
Note: The sum function in Python only works on iterables that contain numerical values, and it will raise a TypeError if it is called on an iterable that contains non-numeric values

Conclusion
In conclusion, the sum function in Python is a versatile tool that can be used in a variety of ways to quickly sum up values in a list, tuple, or other iterable objects. We have also discussed various examples that used the sum function in Python, to sum up the elements present in a list or other iterables.

Frequently Asked Questions(FAQs)

Here are some FAQs related to the sum function in Python

Ques 1. What types of iterables can be passed to the sum function in Python?
Ans. The sum function in Python can take any iterable that contains numbers, including lists, tuples, and sets.

Ques 2. Can the sum function in Python be used with other data types besides numbers?
Ans. No, the sum function in Python can only be used with numerical data types (e.g., integers, floats, etc.). If you try to use the sum function with other data types (e.g., strings), you will get a TypeError.

Ques 3. Can the sum function in Python be used with a list of strings?
Ans. No, we cannot use the sum function in Python with a list of Strings. If we try to use the sum function in Python with a list of strings, it will return a TypeError.

Ques 4. Is the sum function in Python efficient for large lists?
Ans. Yes, the sum function in Python is very efficient, even for very large lists. The sum function is implemented in C, so it is much faster than using a for loop to iterate over the list and add up the elements.

Ques 5. Can the sum function in Python be used to sum elements of a dictionary?
Ans. No, the sum function in Python cannot be used to sum elements of a dictionary.

Leave a Reply

Your email address will not be published. Required fields are marked *