Python NumPy Tutorial | Learn With Examples

In this Python NumPy Tutorial with examples, you will learn what is NumPy? its features, advantages, modules, packages, and how to use NumPy Arrays with sample examples in Python code.

Every sample example explained here is tested in our development environment and is available at PySpark Examples Github project for reference.

All examples provided in this Python NumPy tutorial are basic, simple, and easy to practice for beginners who are enthusiastic to learn NumPy and advance their careers.

Note: In case you can’t find the NumPy examples you are looking for on this tutorial page, I would recommend using the Search option from the menu bar to find your tutorial and sample example code.

What is Numpy Array?

To do

In order to use Numpy, you need to install it first and use the following import.


# Import NumPy package
import numpy as np

What is ndarray (N-dimensional array)?

numpy.ndarray is an array object in Python that is used to represent a fixed size multidimensional array of the same types.

class numpy.ndarray(shape, dtype=float, buffer=None, offset=0, strides=None, order=None)

This constructor of the ndarray is low-level and the users should not use it to create NumPy array.

The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension.

The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray.

Install Python Numpy Library

To do

Create Numpy Array

Though numpy.ndarray provides a constructor to create an array, it is not recommended to use as it is a low-level constructor and recommends to use numpy.array() function to create NumPy array. numpy.array() returns the ndarray object of type numpy.ndarray.


# Create Numpy array
nparray = np.array([[10, 20, 30], [40, 50, 60]]

This creates a NumPy array nparray object. Let’s check the type, shape, and dtype of elements of this array.


# Type of array
type(nparray)
#<class 'numpy.ndarray'>

# Share of array
nparray.shape
#(2, 3)

# Type of elements
nparray.dtype
#dtype('int32')

Access the Array Elements using the Index

Update Array Elements

Convert NumPy Array to Python List

To do

Convert Python List to AnumPy Array

To do

Numpy Array Properties

To do

NumPy Array Functions

To do