NumPy Tutorial

Last Updated : 24 Feb, 2026

NumPy is a core Python library for numerical computing, built for handling large arrays and matrices efficiently. It is significantly faster than Python's built-in lists because it uses optimized C language style storage where actual values are stored at contiguous locations (not object reference). It also supports vectorized computations.

  • ndarray object: N-dimensional array for fast numerical operations.
  • Vectorized operations: Perform element-wise calculations without loops.
  • Broadcasting: Operate on arrays of different shapes.
  • Linear algebra functions: Matrix operations like multiplication and inversion.
  • Statistical functions: Mean, median, standard deviation.
  • Integration: Works with Pandas, SciPy and scikit-learn.

Why Learn NumPy?

  • Executes vectorized operations 10 to 100 times faster than standard Python loops.
  • Uses homogeneous arrays to store large datasets more compactly than Python lists.
  • Provides optimized functions for linear algebra, Fourier transforms and matrix manipulations.
  • Includes robust tools for statistics, random number generation and missing data management.
  • Expresses complex math in single-line commands, eliminating the need for manual, nested loops.

NumPy Basics

This section covers NumPy installation, importing, core features and its advantages over Python lists for numerical computing.

NumPy Arrays

NumPy arrays (ndarrays) are the backbone of the library. This section covers how to create and manipulate arrays effectively for data storage and processing

Python
import numpy as np

a = [9,3,3,5]
print(np.array(a))  # Converting list into numpy array

Output
[9 3 3 5]

Mathematical Operations in NumPy

This section covers essential mathematical functions for array computations, including basic arithmetic, aggregation and mathematical transformations.

Python
import numpy as np

n = np.array([9,3,3,5])
print(n.mean())    # Calculating mean

Output
5.0

Linear Algebra with NumPy

NumPy provides built-in functions for linear algebra operations essential for scientific computing and machine learning applications.

Python
import numpy as np

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

res = np.dot(A, A)    # Matrix multiplication
print(res) 

Output
[[ 7 10]
 [15 22]]

Random Number Generation and Statistics

NumPy’s random module provides a list of functions for generating random numbers, which are essential for simulations, cryptography and machine learning applications.

Python
import numpy as np

a = np.random.normal(0, 1, 5)    # 5 values from normal distribution
print("Data:", a)
print("Mean:", np.mean(a))

Output
Data: [ 0.50380896  1.56391917 -0.25398057 -0.31520396  0.4174568 ]
Mean: 0.38320007938038125

Advanced NumPy Operations

This section includes vectorized operations for speed optimization, memory management strategies and integration with Pandas for efficient data analysis.

Python
import numpy as np

a = np.arange(5)
r = a * 10    # Vectorized operation (fast and efficient)
print(r)

Output
[ 0 10 20 30 40]

Quiz & Practice

Test your knowledge of NumPy with this quiz, covering key topics such as array operations, mathematical functions and broadcasting.

Comment

Explore