function in Python [3 examples]

In this NumPy tutorial, I will explain what the function in Python is, its syntax, parameters required, and some use cases. I will also explain what the np.add.reduce() function in Python with example.

To understand in Python, it is a specialized NumPy function for unbuffered, in-place addition at specific array indices, including handling repeated indices. Like,, [index1, index2], [value1, value2]) adds values at the specified indices of arr. Meanwhile, np.add.reduce() is used for reducing an array’s dimensions by summing its elements along a given axis, such as np.add.reduce(arr, axis=0) for column-wise summation in a 2D array. function in Python

The function in Python is a specialized method offered by NumPy. This is used to perform element-wise operations on arrays. The method provides a way to perform unbuffered in-place addition on an array at specified indices.

NumPy function’s syntax

The basic syntax of function in Python is as follows:, indices, values) function’s parameter


arrThe array in Python to which values will be added. This array is modified in place.
indicesThe locations in the Python array where values should be added. This can be a single integer, a list of integers, or a tuple of integer arrays.
valuesThe values are to be added at the specified indices in the array in Python.
List of parameters required in the function in Python.

NumPy add element to array use cases

The key feature of function in Python is its ability to perform in-place, unbuffered addition at specified indices. This means that if an index is repeated in indices, the corresponding value in values is added multiple times.

Let’s see some use cases of the function in Python:

1. in Python for Simple Addition

Here, we will try to do simple addition with all the parameters of the function in Python.:

import numpy as np

arr = np.array([1, 2, 3, 4, 5]), [0, 2, 4], 10)


[11  2 13  4 15]

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor. function in Python

2. NumPy add at function in Python with Repeated Indices

Here, we will try to add arrays with repeated indices in Python.

import numpy as np

arr = np.array([1, 2, 3, 4, 5]), [0, 0, 2, 2], [1, 2, 3, 4])


[ 4  2 10  4  5]

The output from running the code in PyCharm is visually represented in the screenshot below.

numpy add two arrays using np add at in Python

np.add.reduce() function in Python

The numpy.add.reduce() function in Python applies the add operation repeatedly to the elements of an array, effectively reducing the array’s dimension by one.


numpy.add.reduce(array, axis=0, dtype=None, out=None, keepdims=False, initial)

For example:

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

total_sum = np.add.reduce(arr)
print("Total Sum:", total_sum)

sum_along_axis0 = np.add.reduce(arr, axis=0)
print("Sum along axis 0:", sum_along_axis0)

sum_along_axis1 = np.add.reduce(arr, axis=1)
print("Sum along axis 1:", sum_along_axis1)


Total Sum: [5 7 9]
Sum along axis 0: [5 7 9]
Sum along axis 1: [ 6 15]

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

add two numpy arrays in Python


To summarize, the function in Python is used for unbuffered in-place addition at specified indices of an array, useful for handling repeated indices and accumulating values. And also the np.add.reduce() function, applies addition across an array’s elements to reduce its dimension, ideal for summing values along a specified axis.

Both functions are integral for efficient numerical operations in Python.

You may also like to read: