Problem 1

Import the NumPy library under the alias np.

In [1]:
import numpy as np

Problem 2

Add 2 to every element of the following NumPy array.

In [2]:
arr = np.array([2,3,4,9,7,5,8,2])
arr+2
Out[2]:
array([ 4,  5,  6, 11,  9,  7, 10,  4])

Problem 3

Double the value of every element in arr by adding it to itself.

In [3]:
arr + arr
Out[3]:
array([ 4,  6,  8, 18, 14, 10, 16,  4])

Problem 4

Create an array of zeros by subtracting arr from itself.

In [4]:
arr - arr
Out[4]:
array([0, 0, 0, 0, 0, 0, 0, 0])

Problem 5

Multiply every element in arr by 6.

In [5]:
arr * 6
Out[5]:
array([12, 18, 24, 54, 42, 30, 48, 12])

Problem 6

Divide every element in arr by 4.

In [6]:
arr / 4
Out[6]:
array([0.5 , 0.75, 1.  , 2.25, 1.75, 1.25, 2.  , 0.5 ])

Problem 7

Create an array of ones by dividing every element in arr by itself.

In [7]:
arr / arr
Out[7]:
array([1., 1., 1., 1., 1., 1., 1., 1.])

Problem 8

Calculate the square root of every element in arr. Do not use the ** operator.

In [8]:
np.sqrt(arr)
Out[8]:
array([1.41421356, 1.73205081, 2.        , 3.        , 2.64575131,
       2.23606798, 2.82842712, 1.41421356])