# ARITHMETIC OPERATIONS IN NUMPY

In this tutorial, we are going to learn about the arithmetic operations in numpy and focus on the examples in hand by going through some exercises.

### What are Arithmetic Operations?

After working with string operations it’s time work with arithmetic operation in numpy. Arithmetic operations perform tasks like adding, subtracting, dividing, etc. The arrays in operation must be of the same shape or have at least same broadcasting rules, otherwise you might face an error. ### Basic Arithmetic Operations

Let’s perform some basic arithmetic operations:

```import numpy as np

arr1 = np.arange(10).reshape(2,5)
arr2 = np.array([6,7,8,9,10])

print("Array 1:")
print(arr1)

print("\n")

print("Array 2:")
print(arr2)

print("\n")

print("\n")

#Subtracting two arrays
print("Subtracting two arrays:")
print(np.subtract(arr1,arr2))

print("\n")

#Multiply two arrays
print("Multiply two arrays:")
print(np.multiply(arr1,arr2))

print("\n")

#Dividing two arrays
print("Dividing two arrays:")
print(np.divide(arr1,arr2))

```

Output:

```Array 1:
[[0 1 2 3 4]
[5 6 7 8 9]]

Array 2:
[ 6  7  8  9 10]

[[ 6  8 10 12 14]
[11 13 15 17 19]]

Subtracting two arrays:
[[-6 -6 -6 -6 -6]
[-1 -1 -1 -1 -1]]

Multiply two arrays:
[[ 0  7 16 27 40]
[30 42 56 72 90]]

Dividing two arrays:
[[0.         0.14285714 0.25       0.33333333 0.4       ]
[0.83333333 0.85714286 0.875      0.88888889 0.9       ]]```

Let’s look at some other arithmetic functions that are available on Numpy:

### numpy.reciprocal()

The reciprocal function returns the reciprocal of the values provided element-wise. Let’s look at some examples:

```import numpy as np
arr = np.array([10,20, 33, 40])
print('Array 1 is:')
print(arr)

print("\n")
print("Reciprocal function applied: ")
print(np.reciprocal(arr))
print("\n")

arr2 = np.array([3.5, 6.3, 5.6, 2.3])
print('Array 2 is:')
print(arr2)

print("\n")
print("Floating values reciprocal is: ")
print(np.reciprocal(arr2))```

Output:

```Array 1 is:
[10 20 33 40]

Reciprocal function applied:
[0 0 0 0]

Array 2 is:
[3.5 6.3 5.6 2.3]

Floating values reciprocal is:
[0.28571429 0.15873016 0.17857143 0.43478261]```

### numpy.mod()

The mod function returns the remainder value when two arrays are divided element wise. You can also use the remainder function to produce the same result, for example:

```import numpy as np
arr = np.array([10, 20, 33, 40])
print('Array 1 is:')
print(arr)

print("\n")

arr2 = np.array([3.5, 6.3, 5.6, 2.3])
print('Array 2 is:')
print(arr2)

print("\n")
print("Mod function applied: ")
print(np.mod(arr, arr2))
print("\n")

print("\n")
print("Remainder function applied: ")
print(np.remainder(arr, arr2))
```

Output:

```Array 1 is:
[10 20 33 40]

Array 2 is:
[3.5 6.3 5.6 2.3]

Mod function applied:
[3.  1.1 5.  0.9]

Remainder function applied:
[3.  1.1 5.  0.9]```

There are several arithmetic operations in numpy that we can use for different purposes. You can learn more about them here:

References:
http://scipy-lectures.org/intro/numpy/operations.html