close
close

What is Python’s sum() function and how is it used?


What is Python’s sum() function and how is it used?

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:

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:

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:

Pretty simple.

You can also sum() like this:

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:

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:

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:

You could set an initial value like this:

Complete the code as follows:

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:

It is also possible sum() during use inputbut it is a little more difficult.

What we need to do is first accept the input for our variables as follows:

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:

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:

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.

group Created with Sketch.

Leave a Reply

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