π NumPy for Everyone: From Basics to Advanced

Table of contents
- π§ What is NumPy?
- π― Why Use NumPy?
- π§ Installation and Import
- π¦ NumPy Array (ndarray) β Core Object
- π Important Attributes of NumPy Array (with Examples)
- π¨ Creating Arrays β Methods Youβll Love
- π Array Operations
- 𧬠Reshaping & Indexing
- π§ Broadcasting
- π² Random Arrays
- π Saving and Loading
- π Conclusion: Why Learn NumPy?
π§ What is NumPy?
NumPy (short for Numerical Python) is a powerful Python library used for mathematical and scientific computing. It provides a fast way to work with arrays (lists of numbers), matrices, and numerical functions.
β In simple terms: NumPy helps Python do fast maths with lots of numbers at once.
π― Why Use NumPy?
Python lists are slow for math operations.
NumPy arrays are faster and memory efficient.
NumPy lets you:
Add, subtract, multiply numbers easily
Create matrices, reshape them
Perform statistics and linear algebra
Work with images, signals, data science models, and much more
π§ Installation and Import
To use NumPy, first install it:
pip install numpy
Then import it in your Python file:
import numpy as np
π¦ NumPy Array (ndarray
) β Core Object
The most important thing in NumPy is the ndarray object β this is what stores numbers in a fast, structured way.
Letβs create one:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
Now letβs understand the attributes of this array.
π Important Attributes of NumPy Array (with Examples)
Attribute | Description | Example Output |
ndim | Number of dimensions | 2 for a 2D array |
shape | Tuple showing rows & columns | (2, 3) |
size | Total number of elements | 6 |
dtype | Data type of elements | int64 |
itemsize | Size (in bytes) of each element | 8 (for int64) |
nbytes | Total memory used by array | 48 bytes |
T | Transpose of the array | Converts rows to columns |
Full Example:
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array:\n", arr)
print("Dimensions (ndim):", arr.ndim)
print("Shape:", arr.shape)
print("Total Elements (size):", arr.size)
print("Data Type (dtype):", arr.dtype)
print("Item Size (bytes):", arr.itemsize)
print("Total Bytes (nbytes):", arr.nbytes)
print("Transpose:\n", arr.T)
π¨ Creating Arrays β Methods Youβll Love
np.zeros((2, 3)) # Array of all 0s
np.ones((2, 3)) # Array of all 1s
np.full((2, 2), 7) # Filled with 7
np.eye(3) # Identity matrix
np.arange(0, 10, 2) # From 0 to 10 with step 2
np.linspace(1, 5, 4) # 4 equally spaced values from 1 to 5
π Array Operations
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * b) # [ 4 10 18]
print(np.sin(a)) # Apply sin function to each element
NumPy automatically performs operations element-wise.
𧬠Reshaping & Indexing
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[1]) # [4 5 6]
print(arr[:, 1]) # [2 5] (2nd column)
print(arr[0, 2]) # 3
reshaped = arr.reshape(3, 2)
π§ Broadcasting
Even if two arrays have different shapes, NumPy can sometimes "stretch" them to work together:
a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([1, 2, 3])
print(a + b) # Adds b to each row of a
π² Random Arrays
np.random.rand(2, 3) # Random floats
np.random.randint(0, 10, 5) # Random integers between 0β9
π Saving and Loading
np.save("myarray.npy", arr)
loaded = np.load("myarray.npy")
π Conclusion: Why Learn NumPy?
β Whether youβre into machine learning, data analysis, robotics, or just want to speed up your Python math, NumPy is the first library to learn.
π§ͺ Practice small examples. Try reshaping, slicing, and operating on arrays. You'll see why NumPy is powerful and fun!
Subscribe to my newsletter
Read articles from Nitin Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
