Introduction to Numpy Simple Arithmetic Operators in Python

In This Article, You Will Learn About Numpy Simple Arithmetic Operators

Numpy Simple Arithmetic Operators – Before moving ahead, let’s know a bit of Creation of Own Numpy Universal Function in Python.

While you could use arithmetic operators directly between NumPy arrays this is an extended to the same. Functions that can take any array objects like e.g., lists, tuples, etc. and perform arithmetic conditionally.

Arithmetic Conditionally – Where we can define condition to apply arithmetic operations.

All arithmetic operations take a parameter where as condition to specify to apply arithmetic operations.

Addition

add() – It sums the value of one array to the value of another array.

Example – Adding the values of both arrays.

import numpy as np

array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])

new_array = np.add(array_1, array_2)

print(new_array)
numpy-simple-arithmetic-operators

As a result, it returned an array containing sum of both the arrays.

Subtraction

subtract() – It subtracts the value of one array from the value of another array.

Example – Subtracting the values of both arrays.

import numpy as np

array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])

new_array = np.subtract(array_1, array_2)

print(new_array)
Output -
[-4 -4 -4 -4]

As a result, it returned an array containing subtracted value from first array to second array.

Multiplication

multiply() – It multiplies the value of one array from the value of another array.

Example – Multiplying the values of both arrays.

import numpy as np

array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])

new_array = np.multiply(array_1, array_2)

print(new_array)
Output - 

[5 12 21 32]

As a result, it returned an array containing multiplying the value of given array.

Division

divide() – It divides the value of one array from the value of another array.

Example – Dividing the values of both arrays.

import numpy as np

array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])

new_array = np.divide(array_1, array_2)

print(new_array)
Output - 
[0.2  0.33333333 0.42857143 0.5]

As a result, it returned an array of containing value of 1/5, 2/6 3/7, 4/8.

Power

power() – It raises the value of one array till the power (value) of another array.

Example – Raising the power of first array’s element till the value of second array.

import numpy as np

array_1 = np.array([1, 2, 3, 4])
array_2 = np.array([5, 6, 7, 8])

new_array = np.power(array_1, array_2)

print(new_array)
Output - 
[1 64  2187 65536]

As a result, it returned an array containing raised value of first array.

Remainder

remainder() – It returns the remainder of the values in the first array as comparison to the values in the second array.

Example – Returns the remainder of the values of first array as comparison to value of second array.

import numpy as np

array_1 = np.array([14, 15, 16, 9])
array_2 = np.array([5, 6, 7, 8])

new_array = np.remainder(array_1, array_2)

print(new_array)

As a result, it returned an array containing remainder value of given array.

mod

mod() – It returns the remainder of the values in the first array as comparison to the values in the second array.

Example – Returns the remainder of the values of first array as comparison to value of second array.

import numpy as np

array_1 = np.array([14, 15, 16, 9])
array_2 = np.array([5, 6, 7, 8])

new_array = np.mod(array_1, array_2)

print(new_array)
Output - 

[4 3 2 1]

As a result, it returned an array containing remainder value of given array.

Quotient and Mod

divmod() – It returns both the quotient and the remainder.

Example – Returns the quotient and remainder of the array.

import numpy as np

array_1 = np.array([14, 15, 16, 9])
array_2 = np.array([5, 6, 7, 8])

new_array = np.divmod(array_1, array_2)

print(new_array)
Output -

Quotient - [2 2 2 1]
Remainder - [4 3 2 1] 

As shown above, it returned two arrays containing one as quotient and another is as remainder.

Absolute Values

absolute() – It works as same as Python built-in math function i.e., abs(). It returns the positive values in the array.

Example – Returns the abs() values of the array.

import numpy as np

array_1 = np.array([-1, -3, 0, 1, 3])

new_array = np.absloute(array_1)

print(new_array)
Output -

[1 3 0 1 3]

As shown above, it returned an array containing absolute value of given array.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

2 thoughts on “Introduction to Numpy Simple Arithmetic Operators in Python”

Leave a Comment

Stay in the loop

codingstreets