“import numpy as np” Tutorial

The numpy is a popular Python library that is provided as 3rd party. The numpy provides an array, lists related operations in an easy-use way. In order to use numpy it should be imported by using the ” import numpy” statement. But there is a more practical way to use numpy with the “import numpy as np” where the np can be used to call the numpy library and related functions and data types.

Install numpy

The numpy is an external or 3rd party library which do not provided with python by default. In order to use it we should install by using package managers or pip command.

pip3 install numpy

“import numpy as np”

The numpy should be imported in order to use it. It can be imported by using the import statement and module name like below.

import numpy

But typing the numpy every time we use one of the elements of numpy is not a practical way. So Python provides the alias which can be used to create alias which is generally a shorter name for the specified module. The following import statement can be used with the “as” statement in order to create alias “np” for the numpy.

import numpy as np

a = np.array([1, 2, 3, 4, 5])

“import numpy as np” Equivalent

Python provides the equal sign “=” in order to create alias after importing a library. The equal alias can be used to set np as the numpy alias after importing is like below.

import numpy

np=numpy

a = np.array([1, 2, 3, 4, 5])

Print numpy Version with np

As the created alias np refers to the numpy when we try to print the version of the np the numpy library is printed. The __version__ attribute can be used to print version information of the numpy.

import numpy as np

print(np.__version__)

Leave a Comment