Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

ufunc Simple Arithmetic

Simple Arithmetic

You can directly use arithmetic operators (+, -, *, /) between NumPy arrays, but this section explores an extension that allows functions to accept any array-like objects, such as lists and tuples, and perform arithmetic conditionally.

Arithmetic conditionally refers to the ability to define specific conditions under which arithmetic operations will be executed.

All the arithmetic functions discussed include a where parameter that allows us to specify the condition for the operation.

Addition

The add() function computes the sum of the contents of two arrays and returns the results in a new array.

Example

Add the values from arr1 to the values in arr2:

import numpy as np

arr1 = np.array([101112131415])
arr2 = np.array([202122232425])

newarr = np.add(arr1, arr2)

print(newarr)
The example above will yield [30, 32, 34, 36, 38, 40], which represents the sums of 10+2010 + 20, 11+2111 + 21, 12+2212 + 22, and so on.