Getting Started with NumPy

NumPy (Numerical Python) is a Python library used for scientific computing. It can also be used as an efficient multi-dimensional container for data.

How to use NumPy

To use any library in Python as in most programming languages, we must first import it. To import NumPy you should have installed Python 3.6 and above. To import “numpy” as any other Python modules we use the keyword “import” as follows:

`import numpy as np`

We make a reference to the library as “np” instead of “numpy.” so that we don’t have to use the word “numpy” everywhere in our code, i.e., creating an array: np.array([1, 2, 3]).

In this tutorial, we will be using this method, although we could just import it as `import numpy` and the example for this would be numpy.array([1,2,3]) when creating an array.

If you already know Python and you have used Python lists, the comparison and differences between the two will make you understand NumPy easily and better.

NumPy vs. Lists

NumPy and Lists are similar to each other in the sense that they can both store data, be indexed, and be iterated. However, NumPy:

• uses less memory,
• is faster, and more
• convenient than Lists.

Also, we cannot perform calculations (add, subtract, multiply, divide and exponentiation) on Python Lists but we can on NumPy Arrays.

Examples

In the following examples, In means INPUT and Out is the OUTPUT after running the code snippet.

CategoriesTags

Getting Started With Git and GitHub

Git is essential knowledge for every programmer. As you practice coding, you will begin to…

How to Create a Super Simple Drawing Tool with Paper.JS

What Are We Going to Make? In this tutorial I will guide you through using…