NumPy Hands-On Tutorial -Chapter 1: Introduction to NumPy
NumPy (Numerical Python) is one of the most important libraries in the Python scientific computing ecosystem. It provides powerful tools…
NumPy Hands-On Tutorial -Chapter 1: Introduction to NumPy

Chapter 1: Introduction to NumPy
NumPy (Numerical Python) is one of the most important libraries in the Python scientific computing ecosystem. It provides powerful tools for numerical computation, array manipulation, and mathematical operations. Almost every major scientific or data science library in Python such as Pandas, SciPy, TensorFlow, PyTorch, and scikit-learn, is built on top of NumPy.
This article explains NumPy from the ground up, including why it exists, how it works, and how to set it up practically.
1. What is NumPy
NumPy is a core Python library for numerical computing that provides:
- A powerful N-dimensional array object
- Fast mathematical operations
- Linear algebra functions
- Random number generation
- Statistical operations
- Broadcasting for efficient computation
The central feature of NumPy is the NumPy Array (ndarray).
Unlike normal Python lists, NumPy arrays are:
- Typed
- Memory efficient
- Vectorized (fast operations)
Basic Example
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
Output
[1 2 3 4 5]
<class 'numpy.ndarray'>
This object is the foundation of almost all numerical work in Python.
2. Why NumPy is Used
NumPy was created to solve limitations of Python lists when working with large numerical datasets.
Key Reasons for Using NumPy
+------------------------+-------------------------------------------------------------+
| Feature | Description |
+------------------------+-------------------------------------------------------------+
| Speed | NumPy operations run in compiled C code |
| Memory Efficiency | Arrays store elements in contiguous memory |
| Vectorized Computation | Operations performed on entire arrays |
| Mathematical Tools | Built-in support for algebra, statistics |
| Multi-dimensional Data | Supports 1D, 2D, 3D and higher arrays |
+------------------------+-------------------------------------------------------------+
Example: Adding Two Lists vs NumPy Arrays
Using Python Lists
list1 = [1,2,3]
list2 = [4,5,6]
result = []
for i in range(len(list1)):
result.append(list1[i] + list2[i])
print(result)
Output
[5,7,9]
This requires manual looping.
Using NumPy Arrays
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
result = a + b
print(result)
Output
[5 7 9]
No loop is required.
This is called vectorization.
3. NumPy in the Python Scientific Ecosystem
NumPy is the foundation layer of the scientific Python stack.
Scientific Ecosystem Structure
Python Language
↓
NumPy (Numerical arrays)
↓
SciPy (scientific algorithms)
↓
Pandas (data analysis)
↓
Matplotlib / Seaborn (visualization)
↓
Machine Learning Libraries
(scikit-learn, TensorFlow, PyTorch)
Why Everything Uses NumPy
Because NumPy provides:
- Efficient numerical storage
- Fast mathematical operations
- Memory optimized data structures
Almost every library internally stores data as NumPy arrays.
Example:
- Pandas DataFrame → built on NumPy arrays
- Scikit-learn → expects NumPy arrays
- TensorFlow → similar tensor concept
4. Relationship with Python Lists
Python lists and NumPy arrays both store collections of data, but they behave very differently.
Python List Characteristics
- Can store mixed data types
- Slower for numerical operations
- Stores elements as objects
Example:
my_list = [1, "hello", 3.14, True]
NumPy Array Characteristics
- Stores single data type
- Much faster numerical operations
- Stored in contiguous memory
Example:
import numpy as np
arr = np.array([1,2,3,4])
Memory Layout Difference
Python List:
Pointer → Object
Pointer → Object
Pointer → Object
NumPy Array:
[1][2][3][4][5]
This contiguous storage makes NumPy extremely fast.
Performance Example
import numpy as np
import time
size = 1000000
list_data = list(range(size))
array_data = np.arange(size)
start = time.time()
result = [x * 2 for x in list_data]
print("List time:", time.time() - start)
start = time.time()
result = array_data * 2
print("NumPy time:", time.time() - start)
NumPy will typically be 10–100x faster.
5. Relationship with Pandas and SciPy
NumPy is the foundation on which Pandas and SciPy are built.
NumPy + Pandas
Pandas is used for:
- Tabular data
- Data analysis
- Data cleaning
But internally, Pandas uses NumPy arrays.
Example:
import pandas as pd
import numpy as np
data = np.array([10,20,30])
series = pd.Series(data)
print(series)
Here:
NumPy Array → stored inside Pandas Series
NumPy + SciPy
SciPy provides advanced scientific algorithms:
- Optimization
- Signal processing
- Integration
- Linear algebra
- Statistics
But all SciPy functions operate on NumPy arrays.
Example:
from scipy import linalg
import numpy as np
A = np.array([[1,2],[3,4]])
inverse = linalg.inv(A)
print(inverse)
Ecosystem Relationship
+------------------+-------------------------+
| Library | Role |
+------------------+-------------------------+
| NumPy | Numerical arrays |
| Pandas | Data analysis |
| SciPy | Scientific algorithms |
| Matplotlib | Visualization |
| Scikit-learn | Machine learning |
+------------------+-------------------------+
NumPy sits at the center.
6. Advantages of NumPy Arrays
NumPy arrays provide many advantages over Python lists.
1. High Performance
NumPy operations are executed in optimized C code.
Example:
import numpy as np
arr = np.array([1,2,3,4])
print(arr * 2)
Output
[2 4 6 8]
2. Vectorization
Operations are applied to entire arrays at once.
Example:
arr = np.array([1,2,3,4])
print(arr + 10)
Output
[11 12 13 14]
3. Multi-Dimensional Arrays
NumPy supports:
- 1D arrays (vectors)
- 2D arrays (matrices)
- 3D+ arrays (tensors)
Example:
matrix = np.array([
[1,2,3],
[4,5,6]
])
print(matrix)
4. Broadcasting
Broadcasting allows operations between arrays of different shapes.
Example:
arr = np.array([1,2,3])
print(arr + 10)
5. Rich Mathematical Functions
NumPy includes hundreds of mathematical functions.
Examples:
np.sum()
np.mean()
np.std()
np.sqrt()
np.exp()
np.log()
np.dot()
7. Installing NumPy
NumPy can be installed using pip or conda.
Install Using pip
pip install numpy
Install Specific Version
pip install numpy==1.26.4
Install Using Conda
conda install numpy
Verify Installation
python
Then:
import numpy
If no error occurs, NumPy is installed.
8. Importing NumPy
The standard convention is:
import numpy as np
Explanation:
numpy→ library namenp→ alias
This makes code shorter.
Example:
import numpy as np
arr = np.array([1,2,3])
print(arr)
Without alias:
import numpy
arr = numpy.array([1,2,3])
The alias np is universally used.
9. Checking NumPy Version
It is often necessary to know the version installed.
Method 1
import numpy as np
print(np.__version__)
Example output
1.26.4
Method 2 (Terminal)
pip show numpy
Output includes:
Name: numpy
Version: 1.26.4
Location: ...
10. Setting Up the Working Environment
A proper working environment improves productivity.
Option 1: Python Interpreter
Run Python directly:
python
Then:
import numpy as np
Option 2: Jupyter Notebook (Recommended)
Install Jupyter:
pip install notebook
Run:
jupyter notebook
Create a new notebook and run:
import numpy as np
arr = np.array([1,2,3])
arr
Option 3: VS Code Environment
Steps:
- Install Python extension
- Install Jupyter extension
- Create
.pyfile - Write code
Example:
import numpy as np
data = np.arange(10)
print(data)
Option 4: Virtual Environment (Best Practice)
Create environment:
python -m venv numpy_env
Activate:
Windows
numpy_env\Scripts\activate
Linux/Mac
source numpy_env/bin/activate
Install NumPy:
pip install numpy
Example Practical Setup
import numpy as np
print("NumPy Version:", np.__version__)
data = np.array([10,20,30,40])
print("Array:", data)
print("Sum:", np.sum(data))
print("Mean:", np.mean(data))
Output
NumPy Version: 1.26.4
Array: [10 20 30 40]
Sum: 100
Mean: 25
Summary
NumPy is the foundation of numerical computing in Python.
Key points:
- Provides fast multidimensional arrays
- Enables vectorized operations
- Used by Pandas, SciPy, and machine learning libraries
- Much faster than Python lists for numeric work
- Easy to install and use
- Essential for data science, AI, and scientific computing
Thanks for reading this article, for more such articles follow the RePromptsQuest.
메타데이터
- post_id
- 7ff55fcc5502
- slug
- numpy-hands-on-tutorial-chapter-1-introduction-to-numpy-7ff55fcc5502
- url
- https://medium.com/@repromptsquest/numpy-hands-on-tutorial-chapter-1-introduction-to-numpy-7ff55fcc5502
- canonical_url
- https://medium.com/@repromptsquest/numpy-hands-on-tutorial-chapter-1-introduction-to-numpy-7ff55fcc5502
- author_url
- https://medium.com/@repromptsquest
- status
- ok
- fetched_at
- 2026-08-18 11:00:21