Do you know what the Sum() function is in Python? Have you ever thought about what sum does in Python? Let us try to gain some knowledge about summing in Python. Also, we will try to know how to sum in Python.
Adding numbers or calculating the sum of the numbers is almost an operation that every basic program requires. Whether building a scientific calculator or working with mathematical patterns, there is always a need for sum in Python.
This article will help you, as a beginner in programming, get familiar with the Python sum() function so that you can easily write Python programs and solve coding problems.
If you feel stuck somewhere, you can always reach out to us and we will help you with your programming assignments and homework problems. Now, let’s get started!
Summary Of The Article:
- Python sum() function is an in-built Python function that is used to calculate the sum of a list of numeric values.
- It can also be used for concatenation of tuples or lists in Python.
- Using the Python sum() function helps us to reduce the length of our code and make it more readable.
- We can use the Python sum() function by providing the iterable and the starting point to the method.
What Is Sum() Function In Python?
The sum function is a built-in function for the Python programming language. The main objective of this function is to provide a summation of a list of numbers. You sometimes use the ‘for’ loop to get this type of answer.
But Python is a robust programming language that is being used for developing some big projects. So, for that Python should reduce the burden of the programmers.
The sum function in Python is associated with the list data type. The elements that are inside of a list should be added for the necessity of the program.
For that purpose, you need to use each loop there. In that case, every element inside the list will get added by the starting number. This will take a lot of time. And if the numbers inside of the list are hundreds, then the time consumption will be huge.
To reduce time consumption, you can use this function. This will reduce the program length to a very small size. Would you like to know how to use the Python sum() function?
Read below to know more!
How To Use The Python sum() Function?
The sum() function in Python is an inbuilt function. Like all the functions, sum() also has some parameters. These are called arguments. The first argument of the sum() function will be the iterable name or the list name.
As the second argument, you need to provide the starting variable. The starting variable is a variable that indicates the base value. If you don’t provide any number there, then the program will assume the start value as zero. And it starts its operation.
So, the process of summing in Python can be done in two ways. These ways will depend upon the declaration of the starting variable. So, there are two possible syntaxes for it. Look below –
1. Sum Function Without Start Parameter
Syntax – sum(iterable_name)
Where the iterable_name is to be replaced by the list name or the name of the iterable like tuples or dictionary etc. In this case, the starting value will be 0 and all the numbers will be added to the sum for the iterable.
2. Sum Function With Start Parameter
Syntax – sum(iterable_name , start_value)
Here, the first argument will be the name of the list or iterable, while the start_value represents a variable or value that is to be added to the sum of the list. The return value for it is the sum of elements of iterable + the start_value.
How To Use The Python sum() Function With Iterables - Using Examples
Let’s use both of these methods of the Python sum() function with different iterable data types. We will see how we can use it with lists, dictionaries, and tuples.
Let us see one example of each data type by using the Python sum() function without the start parameter and the Python sum() function with the start parameter. Keep reading to know!
Using sum() Function With List
In our first example, we will use the sum() function with a list in Python. Let us take a simple list that has some integer values. You can also use floating point values for this.
Now, we will use the Python sum() function with and without the start parameter to find the sum of the numbers in the list. The example code is given below:
sumList = [10,20, 30, 40, 50]
#the sum should return a value of 150
# using the sum() function without start
sum_no_start = sum(sumList)
print("Python sum function without start value: ", sum_no_start)
print()
# using the sum() function with iterable and start parameters
sum_with_start = sum(sumList , 50)
# now, the return value should be 200
print("Python sum function with start value: ", sum_with_start)
In the above program, we have used the Python sum() function using both approaches. Let us check out its output to get to know what the result is.
Output:
From the output, we can see that when we did not use the starting parameter, the sum we got was just the addition of the data elements in the list. But, when we used the function sum() with both parameters, providing the value 50, we saw that the result gave us the sum of elements + 50, which is 200.
Using sum() Function With Dictionary
You must be wondering, “How does sum work with dictionaries in Python?” Well, we are right here to show you how you can do so!
Let us take an example dictionary and use the function sum() on it. The program for it is given below. Check it out!
# take an example of a dictionary with student marks in different subjects
marks = {"english": 90, "maths": 70, "science": 80}
#using the sum function without start value
total = sum(marks.values())
print("Total marks: ", total)
print()
#using sum function with start value adding 10 marks as grace marks
total_new = sum(marks.values(), 10)
print("Total marks including grace marks: ", total_new)
In the above program, we have a dictionary of the marks of a student in different subjects. We have used the function sum() to calculate the total. In the second scenario, we added 10 marks as grace marks to add numbers.
Output:
It is clear in the output that the marks secured is 240 but after including 10 grace marks, it comes out to be 250.
Using sum() Function With Tuples
In Python, tuples are immutable however, we can still perform operations like the sum() function on it. Let’s see an example below.
my_tuple = (1,2,3,4,5)
#using the sum function without the start value
sum_tuple = sum(my_tuple)
print("Sum without start: ", sum_tuple)
print()
#using sum function with the start value
sum_new = sum(my_tuple, 10)
print("Sum with start: ", sum_new)
Like the other examples, we have calculated the sum of the tuple using both the sum() function methods. Let’s take a look at the result to check if it worked.
Output:
In the first case, only the elements of the tuple were added. But, in the second case, the sum of the numbers also included 10, which was the start value.
I think that by now you must have grasped the concept of the Python sum() function.
Conclusion:
As we saw, it is very important to know how to use the Python sum() function.
We need to clarify the concept of what a does sum in Python. Also, along with that, we need to practice how to sum in Python more & more.
It is advisable to clear the basics of the Python programming language. This will help to understand the topic in a better way. This will help us a lot in the future.
So, hope you have liked this piece of article. Share your thoughts in the comments section and let us know if we can improve more.
DMCH also offers a wide range of programming and coding help services for you to benefit from. Don’t miss out! Visit domycodinghomework.com and our expert team will be there to help you out.
Takeaways:
- The Python sum() function is an in-built function that is used to calculate the sum of the numbers in an iterable.
- It is mostly used with lists. However, we can also use it with other data types like dictionaries, Python tuples, sets, etc.
- If you are working with 2D lists and need to find the sum of each sublist, you can use the sum() function with list comprehension.



