In Python you could always do something like this:
The above code is simple: it adds 3 to 5 and prints the result. You could also do something like this:
Again, Python will calculate the sum and print the value. And since adding multiple numbers is a commonly used step in calculations, it’s pretty handy that Python can do this.
You could even use a for loop to sum a sequence of numbers like this:
a = (3, 5, 7, 10) total = 0 for number in a: total += print number(total)
A = (3, 5, 7, 10) in total = 0 for number In A: in total += number press(in total) |
The above code would output the same value as the two lines of code above. In the above code total
is initialized to 0 and then acts as an accumulator as the variable loops through. We could also create a function that does the same thing and can be reused throughout the application. Such a function might look like this:
def total_numbers(a): total = 0 for number in a: total += number returns total print(total_numbers((3, 5, 7, 10)))
In any Total(A): in total = 0 for number In A: in total += number return in total press(Total((3, 5, 7, 10))) |
However, there is a built-in feature that is much better to use. The feature in question is sum()
. One of the main reasons for using this feature is readability. Instead of having to write for
Loops or recursions, sum()
takes care of all this for you.
The sum()
The function was only introduced in Python 2.3 and serves as a Python solution for summing numbers.
Using sum()
is very simple. For example:
a = Sum((3, 5, 7, 10)) print(a)
A = sum((3, 5, 7, 10)) press(A) |
Pretty simple.
You can also sum()
like this:
a = (3, 5, 7, 10) Result = Sum(a) print(“The sum of the numbers = “, Result)
A = (3, 5, 7, 10) Result = sum(A) press(“The sum of the numbers = “, Result) |
The sum()
The function can be used with lists, tuples, sets, ranges and dictionaries.
Let me show you what each of these could look like:
- List: Sum((3, 5, 7, 10))
- Tuple: Sum((3, 5, 7, 10))
- Quantity: Sum({3, 5, 7, 10})
- Range: Sum(Range(3, 10))
- Dictionary: sum({3: “three”, 5: “five”, 7: “seven”, 10: “ten”})
Except for the range, the sum of all the above values is 25. The range, on the other hand, sums up to 42.
You can also get a little more sophisticated with your math. For example, let’s say you want to calculate the sum of the squares of a range of values. It might look something like this:
Sum((x ** 2 for x in range(1, 10)))
sum((X ** 2 for X In reach(1, 10))) |
The sum of the above is 285.
The sum()
The function has two arguments: the required iterable (the numbers to be iterated) and the optional start
. The start
Argument is very handy. Suppose you want to join a list of iterables, but start with a predefined starting value. Without the start
Argument, the starting value is always assumed to be 0. Look at the following line:
The sum is 25 because our starting value is the default value 0. But what if we want to set our starting value to 100, for example? For this you could use the start
Argument as follows:
Sum((3, 5, 7, 10), Start=100)
sum((3, 5, 7, 10), start=100) |
Our total is now 125.
But why don’t you just add the 100 to the list like this:
You could do that and it would work well. But let me give you an example where the use of the start
Argument explains why it may be necessary.
Let’s say you want to add the total amount of a sale, but first apply a discount. For example, the discount could be $200.00. How would you do that? With the start
Argument.
Let’s say you have a list of dollar amounts that looks like this:
Sales = (3000, 5000, 7000, 10000)
Sales = (3000, 5000, 7000, 10000) |
You could set an initial value like this:
Complete the code as follows:
Sales = (3000, 5000, 7000, 10000) Initial value = -200 Total sales = Sum(Sales, Initial value) Print(“Total sales:”, Total sales)
Sales = (3000, 5000, 7000, 10000) Initial value = –200 Total sales = sum(Sales, Initial value) press(“Total sales amount:”, Total sales) |
The output of the above command would be:
Total sales amount: 24800.
What we did above was to define a variable to be used for the starting value and then use it for the total_sales
Summation. We could always initial_value
Variable and code the starting value as follows:
Total sales = Sum(Sales, Start=-200)
Total sales = sum(Sales, start=-200) |
It is also possible sum()
during use input
but it is a little more difficult.
What we need to do is first accept the input for our variables as follows:
a = int(input(“Enter a number: “)) b = int(input(“Enter a number: “)) c = int(input(“Enter a number: “))
A = int(Entrance(“Enter a number: “)) B = int(Entrance(“Enter a number: “)) C = int(Entrance(“Enter a number: “)) |
Next, we define a new variable from the user input as follows:
We can then compare the entered numbers with the sum()
Function:
Finally, we print it out:
The entire code looks like this:
a = int(input(“Enter a number: “)) b = int(input(“Enter a number: “)) c = int(input(“Enter a number: “)) my_list = (a, b, c) total = sum(my_list) print(total)
A = int(Entrance(“Enter a number: “)) B = int(Entrance(“Enter a number: “)) C = int(Entrance(“Enter a number: “)) my_list = (A, B, C) in total = sum(my_list) press(in total) |
We could also use sum()
to calculate the average of numbers. This also requires the len()
Function that takes an object as an argument and returns the length of that object. This code looks like this:
Numbers = (3, 5, 7, 10) Num = Sum(Numbers) Average = Num/Lent(Numbers) Print(Average)
Pay = (3, 5, 7, 10) number = sum(Pay) Average= number/len(Pay) press (Average) |
What is described above is the sum of the numbers from num = sum(numbers)
by the length of the list (which is 4). So 25 divided by 4 is 6.25.
The Python sum()
This feature will be very useful to you on your further journey with this widely spoken language.
YOUTUBE.COM/THENEWSTACK
Technology is evolving fast, don’t miss an episode. Subscribe to our YouTube channel to stream all our podcasts, interviews, demos and more.
SUBSCRIBE