# Sum of the range of values

The sum of a range of numbers is simply the result of adding up all the numbers within a specific range, including both the start and end values. This operation is frequently used in programming and data analysis to compute the total of a subset of data. NumPy provides various methods to calculate the sum of a range of numbers. In this thread, we will discuss some examples of calculating the sum of the range of array values.

#### 1. Using "sum()" function:

NumPy’s `sum()` function is the simplest method of calculating the sum of a range of numbers. It takes an array as input and returns the sum of all the elements in the array.

Let’s understand it better by example given below:

The above code creates an array using NumPy’s `arange` function, which generates a sequence of numbers from 1 to 10. Then it calculates the sum of all the elements in the array using NumPy’s `sum` function, and stores it in a variable called `sum` . Finally, it prints the sum to the console.

NumPy’s `add.reduce()` function is similar to the `sum()` method. This function applies the add function to all the elements in the array and returns the final result.

Using `add.reduce` is an alternative way to calculate the sum of an array in NumPy. It’s similar to using the `sum` function, but it can be more efficient for large arrays.

let’s see the example given below to gain better understanding:

The above code creates an array using NumPy’s `arange` function, which generates a sequence of numbers from 1 to 10. Then it calculates the sum of all the elements in the array using NumPy’s `add.reduce` function, which reduces the array to a single value by applying the addition operator repeatedly. The result is stored in a variable called `sum` . Finally, it prints the sum to the console.

#### 3. Using "cumsum()" function:

NumPy’s `cumsum()` calculates the cumulative sum of the array. It returns an array of the same size as the input array where each element is the sum of all the elements before it. You can use slicing to find the sum.
Using `cumsum` is an alternative way to calculate the sum of an array in NumPy. This can be useful in some situations where you need to access the intermediate results of the cumulative sum.

let’s see the example given below to gain better understanding:

The above code generates an array of numbers from 1 to 3 using NumPy’s `arange` function. It then uses the `cumsum` function to compute the cumulative sum of the array and stores it in a variable called `cumsum` . The sum of all the elements in the original array is obtained by selecting the last element of the `cumsum` array, which is then stored in a variable called `sum` . Finally, the value of `sum` is printed to the console.