NumPy cheatsheet: Array creation and properties

NumPy is a powerful numerical computing library in Python, providing support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays. This cheat sheet serves as a quick reference guide for essential NumPy functionalities, covering array creation, manipulation, indexing, basic operations, linear algebra, and more.

Whether you're a beginner getting started with NumPy or an experienced user looking for a handy reference, this cheat sheet will help you efficiently perform various numerical computations using NumPy in Python.

Import NumPy

To start using NumPy library, first, import it using the following command:

import numpy as np
Importing NumPy library

After importing it, we can begin working with arrays and further operations.

Array creation

NumPy provides multiple ways to create arrays, and their usage depends on the specifics of the tasks.

  1. From Python list

We can create a NumPy array from a pre-existing list using the following function:

my_list = [1, 2, 3, 4, 5]
a = np.array(my_list)
print(a)
  1. Using arange

With the following function, we can create a NumPy array from a range of numbers:

start = 1
stop = 10
step = 1.5
b = np.arange(start, stop, step)
print(b)
  1. Using linspace

With the following function, we can create a NumPy array with evenly spaced values over a specified interval:

start = 1
stop = 10
num = 5
c = arr = np.linspace(start, stop, num)
print(c)
  1. Using zeros

With the following function, we can create a NumPy array of a specific shape, initialized with zeros:

shape = 10
d = np.zeros(shape)
print(d)
shape = (2,2)
d = np.zeros(shape)
print(d)
  1. Using ones

With the following function, we can create a NumPy array of a specific shape, initialized with ones:

shape = 10
e = np.ones(shape)
print(e)
shape = (2,2)
e = np.ones(shape)
print(e)
  1. Using random

With the following function, we can create a NumPy array of a specific shape, initialized with random values:

shape = (2,2)
f = np.random.random(size=shape)
print(f)
  1. Using full

With the following function, we can create an array initialized with a constant value:

g = np.full((2,3), 7)
print(g)
  1. Using eye

With the following function, we can create an identity matrix of our predefined dimension:

h = np.eye((2))
print(h)
  1. Using empty

With the following function, we can create an empty NumPy array:

shape = (3,2)
i = np.empty(shape)
print(i)

Array properties

A NumPy array has multiple properties and when working with arrays, it is helpful to access them to understand the array better. Some of them are:

print(a.shape) # Print shape of array
print(b.ndim) # Print number of dimensions
print(c.size) # Print number of elements
print(d.dtype) # Print the data type of array
print(d.astype(int)) # Convert the array to a different datatype
print(len(e)) # Print length of the array

Array indexing and slicing

There are various functions in NumPy that facilitate array indexing and slicing, providing flexibility and convenience in accessing and manipulating data stored within NumPy arrays. They are listed below:

print(e[0]) # Access element at index 0
print(f[0][1]) # Access element at row 0 and column 1
print(f[:1]) # Access all elements at row 0
print(a[a >= 3]) # Boolean indexing with a condition to access all elements greater than or equal to 3
print(a[::-1]) # Reverse the array

Reshaping arrays

There are built-in functions in NumPy that are used to change the shape of an array, such as:

print(a.reshape(2,3)) # Reshape array from 1D to 2D
print(g.ravel()) # Flatten an array from 2D to 1D

This cheat sheet covers various NumPy functionalities that are used to create arrays and understand them to further utilize them efficiently in tasks.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved