NumPy Tutorial for Beginners

NumPy Tutorial for Beginners

If you want to make a career in big data, you need to learn NumPy. Read on to get started with one of Python's most popular libraries.

I will walk you through the basics of NumPy. If you want to do machine learning then knowledge of NumPy is necessary. It one of the most widely used Python libraries. It is the most useful library if you are dealing with numbers in Python. NumPy guarantees great execution speed compared to standard Python libraries. It comes with a great number of built-in functions.

Advantages of using NumPy with Python:

  • Array-oriented computing.
  • Efficiently implemented multi-dimensional arrays.
  • Designed for scientific computation.

First, let’s talk about its installation. NumPy is not part of the basic Python installation. We need to install it after the installation of Python in our system. We can do it by the pip using command, pip install NumPy, or by installing Conda.

We are done with the installation and now we can jump right into NumPy. First, let’s start with the most important object in NumPy, the ndarray or multi-dimensional array. A multi-dimensional array is an array of arrays. In multi-dimensional arrays, this array, [1,2,3], is a one-dimensional array because it contains only one row. The below is array is a two-dimensional array, as it contains multiple rows as well as multiple columns.

[[1 2 3]

[4 5 6]

[7 8 9]]

Let’s do some coding now. Here I am using Jupyter Notebook to run my code; you can use any IDE available and best suited to you.

We start with import NumPy.

In the following code, I am renaming the package to np for convenience sake.

import numpy as np

Now, in order to create an array in NumPy, we use its array function as shown below:

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


Output: [1 2 3]

This an example of a one-dimensional array.

Another way to create an array in NumPy is by using the zeros function.

zeros = np.zeros(3)


Output: [0. 0. 0.]

If you look closely at the output, the generated array contains three zeros, but the type of the value is a float and, by default, NumPy creates the array of float values.


Output: numpy.float64

Going back to the first example inside NumPy’s array function, we pass a list so we can also pass the list variable inside the array function and the output will be the same.

my_list = [1,2,3]

array = np.array(my_list)


Output: [1 2 3]

Now, let’s look into how to create a two-dimensional array using NumPy. Instead of passing the list now we have to pass a list of tuples or list of lists as mentioned below.

two_dim_array = np.array([(1,2,3), (4,5,6), (7,8,9)])



[[1 2 3]

[4 5 6]

[7 8 9]]

Note that the number of columns should be equal, otherwise NumPy will create an array of a list.

arr = np.array([[1,2,3], [4,6], [7,8,9]])


Output: [list([1, 2, 3]) list([4, 6]) list([7, 8, 9])]

Now, to create an array of a range, which is very good for making plots, we use the linspace function.

range_array = np.linspace(0, 10, 4)


Output: [ 0. 3.33333333 6.66666667 10. ]

Here, the first argument is the starting point and next is the endpoint and the last argument defines how many elements you want in your array.

Now, to create random arrays we can use the random function. Here, I’ve created an array of random integers, and, therefore, used randint where first I specified the maximum value and then the size of my array.

random_array = np.random.randint(15, size=10)


Output: [ 7 11 8 2 6 4 9 6 10 9]

Now we know the basics of how to create arrays in NumPy. Now let’s look into some of its basic operations. First, we will start by finding the size and shape of an array. Size will give the number of elements in an array whereas shape will give us the shape of an array.

For a one dimensional array, the shape would be (n, ), where n is the number of elements in your array.

For a two dimensional array, the shape would be (n,m), where n is the number of rows and m is the number of columns in your array


Output: 3


Output: (3,)


Output: 9


Output: (3, 3)

If we want to change the shape of an array we can easily do it with the reshape function. It will look like something like this:

two_dim_array = np.array([(1,2,3,4), (5,6,7,8)])

two_dim_array = two_dim_array.reshape(4,2)



[[1 2]

[3 4]

[5 6]

[7 8]]

We need to make sure that the rows and columns can be interchangeable. For example, here, we can change rows and columns from (2,4) to (4,2) but can not change them to (4,3) because, for that, we’d need 12 elements and we have only 8. Doing so will give an error as shown below.

ValueError: cannot reshape array of size 8 into shape (4,3)

To check the dimensions of our array. we can use the ndim function.


Output: 2

Now, to get values from an array, a process known as slicing can be done in various ways. For example, array[1] will fetch the second element of my array, but if we want a range we can use array[0:1], which will give us the first two elements. For the last value of the array, we can use array[-1], which is similar to the standard method of getting elements from a list in Python.

Now to find the sum all we have to use is the sum(), function but if we want to find the sum of the axis we can pass an argument for the axis.


Output: [ 6 8 10 12]


Output: [10 26]

Now to add two arrays all we have to use if + operator. For example:

print(two_dim_array + two_dim_array)


[[ 2 4 6 8]

[10 12 14 16]]

Similarly, we can use other operands as well, like multiple, subtract, and divide.

We have many other operations present in NumPy like sqrt, which will give us the square root of every element, and std, which is used to find the standard deviation. To explore more about these operations visit the NumPy’s documentation.

And that’s it for the introduction of NumPy.

Learn More

Learn Programming with Python Step by Step

MySQL Databases With Python Tutorial

Creating Web Sites using Python and Flask

Complete Python: Go from zero to hero in Python

An A-Z of useful Python tricks

A Complete Machine Learning Project Walk-Through in Python

Learning Python: From Zero to Hero

MongoDB with Python Crash Course - Tutorial for Beginners

Introduction to PyTorch and Machine Learning

Originally published by Prabhat Kashyap at

numpy python

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

NumPy Array Tutorial - Python NumPy Array Operations and Methods

Learn about NumPy Array, NumPy Array creation, various array functions, array indexing & Slicing, array operations, methods and dimensions,It also includes array splitting, reshaping, and joining of arrays. Even the other external libraries in Python relate to NumPy arrays.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Introduction to Python NumPy | What is Python NumPy

The NumPy in Python explains what exactly is Numpy and how it is better than Lists. It also explains various Numpy operations with examples.

NumPy Applications - Uses of Numpy

Learn the uses of numpy - Alternate for lists in python, multi dimensional array, mathematical operations. See numpy applications with python libraries.

NumPy Installation - How to Install Numpy in Python

Python is an open-source object-oriented language. It has many features of which one is the wide range of external packages. There are a lot of packages for installation and use for expanding functionalities. These packages are a repository of functions in python script. NumPy is one such package to ease array computations.