Medium“Lets ’s Learn NumPy: From basics to powerful Array in Python” | by roshni kumari | Medium
Sitemap
3 min readApr 18, 2026

--

“Lets ’s Learn NumPy: From basics to powerful Array in Python”

“Understand arrays, vectorization, and why NumPy is essential for fast numerical computing.”

NumPy (Numerical Python) is one of the most essential libraries. It is designed for fast numerical computations and provides support for large, multi-dimensional arrays and matrices, along with a wide collection of mathematical functions.

In this blog, we’ll dive deep into why NumPy is essential, how it differs from standard Python lists, and the powerful operations it enables.

1. What is NumPy?

NumPy provides powerful N-dimensional arrays (ndarray). Unlike standard Python, it is:

  • Fast →making operations much quicker than Python lists
  • Efficient → Uses less memory due to optimized storage
  • Easy to use → Simple syntax and powerful operations
  • Interoperable → Works well with other libraries like Pandas, Matplotlib, etc.
  • Mathematically powerful → Built-in support for linear algebra, statistics, and more

Key Concept: N-Dimensional Arrays

The core of NumPy is the ndarray (N-dimensional array).

  • 1D → Vector
  • 2D → Matrix
  • 3D+ → Higher-dimensional data

EXAMPLE:

import numpy as np

# Creating a basic 1D array
arr = np.array ([1, 2, 3])
print(arr)

NumPy Arrays vs Python Lists

Press enter or click to view image in full size

NumPy stores data in continuous memory blocks, making it more efficient.

Memory Comparison

NumPy arrays are significantly more memory-efficient. You can check this using:

import sys
import numpy as np
py_list = list(range(1000))
print("Python list size:", sys.getsizeof(py_list))

np_arr = np.array(range(1000))
print("NumPy array size:", np_arr.nbytes)

Performance Comparison

Let’s compare Python list vs NumPy array.

Python List

import time
size = 1000000
lst = list(range(size))
start = time.time()
result = [x**2 for x in lst]
end = time.time()
print("Time:", end - start)

NumPy Array

arr = np.array(lst)
start = time.time()
result = arr ** 2
end = time.time()
print("NumPy Time:", end - start)

👉 NumPy is significantly faster due to vectorized operations.

Creating NumPy Arrays

There are several ways to initialize arrays depending on your needs:

  • From Lists: np.array([1, 2, 3])
  • Zeros: np.zeros((3, 4)) — Creates a 3 *4 array of 0s.
  • Ones: np.ones((3, 3)) — Creates a 3 *3 array of 1s.
  • Constant Value: np.full((2, 3), 5) — Creates a 2 *3 array filled with 5s.
  • Identity Matrix: np.eye(3) — 3 *3 matrix with 1s on the diagonal.
  • Range: np.arange(1, 20, 2) — Elements from 1 to 20 with a step of 2.
  • Linearly Spaced: np.linspace(0, 10, 5) — 5 evenly spaced values between 0 and 10.

4. Array Properties & Manipulation

Understanding the “shape” of your data is crucial.

Key Properties:

  • shape: Dimensions of the array (e.g.,(4, 3)).
  • size: Total number of elements.
  • ndim: Number of dimensions.
  • dtype: Data type of elements (e.g., int64, float64).
  • itemsize: Size of each element in bytes.
arr = np.array([[1, 2, 3], [4, 5, 6]])
  • arr.shape → (rows, columns)
  • arr.dtype → data type
  • arr.ndim → number of dimensions

Reshaping & Flattening

You can change the structure of an array without changing its data:

Get roshni kumari’s stories in your inbox

Join Medium for free to get updates from this writer.

Python

arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3) # Converts to 2 rows, 3 columns
flattened = reshaped.flatten() # Converts back to 1D

Vectorization & Broadcasting

This is where NumPy shines.

Vectorization

Instead of looping through an array to square every number ,this performs the operation on the entire array at once .

import numpy as np

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

result = arr * 2

print(result)

Broadcasting

Broadcasting allows NumPy to perform arithmetic on arrays of different shapes.

Rule: Arrays can be “stretched” to match if their dimensions are equal or if one of them is 1.

import numpy as np

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

result = arr + 5

print(result)

  • Vectorization → removes loops → faster code
  • Broadcasting → handles different shapes automatically.
Press enter or click to view image in full size

Key Advantages of NumPy

  • Faster computations
  • Reduced memory usage
  • Cleaner and shorter code
  • Ideal for large datasets
  • Foundation for ML libraries (like TensorFlow, Scikit-learn)

When Should You Use NumPy?

Use NumPy when:

  • You are working with large numerical datasets
  • You need high performance
  • You want to avoid loops
  • You are doing mathematical or statistical operations

#NumPy #Python #DataScience #MachineLearning #LearnPython

--

--

roshni kumari
roshni kumari

Written by roshni kumari

0 followers

AI/ML | Data Science| Curious Learner Exploring Machine Learning,Data Science,through projects and honest learnings. Documenting my journey one build at a time.