NumPy is a library in Python that helps us create and use arrays instead of
lists. You might be wondering, why do we need to use arrays, when we have
predefined lists.
The thing is, lists are quite slow to process, and using NumPy arrays can
increase the speed of the program by around 50 times.
Installing NumPy
NumPy doesn’t come pre-installed with the Python library, so you need to install it separately.
The steps to install NumPy are common for MacOS, Linux and Windows after opening the required application.
Windows Users-
- Step-1: Search for an application called “Command Prompt” and open it.
MacOS/Linux users
- Step-1: Click the Command key and spacebar together and type “Terminal” in it. Open the application called “Terminal”.
-
Step-2: Check if NumPy is already installed by typing
pip show numpy
and pressing enter. If it’s not working, typepip3 show numpy
. This is because you have Python3 installed. -
Step-3: If it is installed, you will see the version, license, etc.
of NumPy. If it is not installed, you will see a message saying something
like
WARNING: Package(s) not found: numpy
. -
Step-4: Install Numpy using
pip install numpy
orpip3 install numpy
, depending upon your Python version -
Step-5:You are good to go. Just remember to import the library in the
program using
import numpy as xyz
. Substitutexyz
with any variable of your choice.
Importing NumPy
You can import NumPy in your program using the
import
keyword.
So, the first line of the code will be import numpy
.
Importing NumPy as x
You can import NumPy under an alias. Usually, the alias np
is
used, but it's your choice what alias you want to use.
Creating a NumPy array
A NumPy array can be created in the following way-
import numpy as x
arr=x.array([1,2,3,4,5])
print(arr)
print(type(arr))
The output-
[1 2 3 4 5]
<class 'numpy.ndarray'>
That is all for this post, follow this blog for more posts on NumPy and Python.
Visit my Medium account at aaraviyer10.medium.com!
Happy coding,
Aarav Iyer