Arithmetic Operations in Numpy Arrays
Numpy - Arithmetic Operations

Arithmetic Operations in Numpy Arrays

Arithmetic Operations in Numpy

As we already know the arithmetic operations like addition, subtraction, division, modulo division, floor division, etc. in python. The same arithmetic operations can be performed in the Numpy arrays.


Create an Numpy arrays

import numpy as np
data1 = np.array([1,2,3])
data2 = np.array([4,5,6])        

Here, we create an arrays named 'data1' and 'data2' to perform arithmetic operations on it.


Addition in Numpy arrays

We can add two numpy arrays by using the symbol '+' is placed in between the two arrays.

Example

print("Addition :", data1 + data2)        

Output

Addition : [5 7 9]        


Subtraction in Numpy arrays

We can subtract two numpy arrays by using the symbol '-' is placed in between the two arrays.

Example

print("Subtraction :", data1 - data2)        

Output

Subtraction : [-3 -3 -3]        


Multiplication in Numpy arrays

We can multiply two numpy arrays by using the symbol '*' is placed in between the two arrays.

Example

print("Multiplication :", data1 * data2)        

Output

Multiplication : [ 4 10 18]        


Division in Numpy arrays

We can divide two numpy arrays by using the symbol '/' is placed in between the two arrays. It will return quotient as a result.

Example

print("Division :", data1 / data2)        

Output

Division : [0.25 0.4  0.5 ]        


Modulo Division in Numpy arrays

We can divide two numpy arrays by using the symbol '%' is placed in between the two arrays. It will return remainder as a result.

Example

print("Modulo Division :", data1 % data2)        

Output

Modulo Division : [1 2 3]        


Floor Division in Numpy arrays

We can divide two numpy arrays by using the symbol '//' is placed in between the two arrays. It will neglect the values after a decimal.

Example

print("Floor Division :", data1 // data2)        

Output

Floor Division : [0 0 0]        


Exponent in Numpy arrays

We can take exponent of two numpy arrays by using the symbol '**' is placed in between the two arrays.

Example

print("Exponent :", data1 ** data2)        

Output

Exponent : [  1  32 729]        

Challenge: Try out the all above operations in 2D array and see the output.

Follow on LinkedIn - Trillionaire TK

Follow on Instagram - Trillionaire TK

Visit our website - Information Technology

Learn, Code, Practice!!!

To view or add a comment, sign in

More articles by Tharunkumar S

  • Comparison Operations in Numpy Arrays

    Comparison Operations in Numpy As we already know the comparison or relational operations like less than, less than and…

  • Shape of an Array and Reshape() function

    Shape of an Numpy array In Numpy array, shape is an attribute that is used to check the dimension of an array. shape…

  • Slicing in Numpy Arrays

    What is Slicing in Numpy Array? Slice the array in a particular range of elements. In other words, Make an sub array…

  • Indexing in Numpy Arrays

    What is Indexing in Numpy Array? Access the elements in a Numpy array based on index values. Accessing the elements in…

  • Explore array(), type() and astype() functions

    What is ndarray in Numpy? "ndarray" stands for n-dimensional array. Numpy creates an object for an array in this type.

  • Introduction to Numpy Library in Python

    Introduction NUMPY stands for Numerical Python. Numpy is one of the libraries for the python programming language.

  • Journey of Machine Learning Begins

    Machine Learning Definition : The term Machine Learning was coined by Arthur Samuel in 1959, an American pioneer in the…

Others also viewed

Explore content categories