NumPy is the backbone of Data Science. Almost every popular Python library, including Pandas, Matplotlib, SciPy, Scikit-learn, TensorFlow, PyTorch, and OpenCV, is built using NumPy. Before learning any advanced library, it is essential to understand how NumPy works.
Imagine you have data containing the marks of one million students. If you store this data in a normal Python list and perform calculations, the program will take more time and consume more memory. NumPy solves this problem by introducing highly optimized arrays that perform numerical calculations significantly faster than Python lists.
Whether you want to become a Data Scientist, Machine Learning Engineer, AI Developer, Data Analyst, Researcher, or Software Engineer, NumPy will be one of the most frequently used libraries in your career.
In this tutorial, you will learn everything needed to start your NumPy journey, including installation, importing, creating arrays, understanding how arrays differ from Python lists, and accessing array elements using indexing.
What is NumPy?
NumPy stands for Numerical Python. It is an open-source Python library designed specifically for numerical computing, scientific calculations, and high-performance array operations.
Unlike Python lists, NumPy stores data inside a special object called an ndarray, which stands for N-dimensional Array. These arrays are optimized for speed and memory efficiency.
Because NumPy arrays are stored in contiguous memory locations, operations such as addition, subtraction, multiplication, division, statistics, and linear algebra can be performed much faster than using traditional Python lists.
NumPy is considered the foundation of the Python Data Science ecosystem because almost every advanced library depends on it internally.
Today, NumPy is widely used in Data Science, Machine Learning, Artificial Intelligence, Computer Vision, Image Processing, Robotics, Scientific Research, Engineering, Financial Analysis, Big Data Analytics, and Deep Learning.
Why Do We Need NumPy?
Python lists are excellent for general programming because they are flexible. A single Python list can contain integers, floating-point numbers, strings, Boolean values, and even other lists.
For example:
student = ["Aditya", 22, 82.5, True]
This flexibility is useful, but it comes at a cost. Python lists are not designed for high-speed numerical calculations.
Suppose you need to calculate the average marks of ten million students. A normal Python list requires much more memory and performs mathematical operations more slowly.
NumPy was created to solve this problem.
NumPy stores numerical data efficiently and performs operations using highly optimized C-based implementations behind the scenes. As a result, NumPy arrays are significantly faster and consume less memory.
Another powerful feature of NumPy is vectorization.
Instead of writing loops to perform calculations element by element, NumPy performs operations on the entire array using a single statement.
For example, adding 10 to every value in an array requires only one line of code.
import numpy as np marks = np.array([70, 80, 90, 85]) updated_marks = marks + 10 print(updated_marks)
Output
[80 90 100 95]
Notice that we did not write any loop. NumPy automatically applied the operation to every element.
Features of NumPy
NumPy offers many features that make it one of the most powerful libraries in Python.
It performs calculations much faster than Python lists because it is optimized using low-level programming languages like C.
It uses less memory by storing elements of the same data type together in contiguous memory locations.
It supports one-dimensional, two-dimensional, and multidimensional arrays, making it suitable for everything from simple calculations to complex scientific simulations.
NumPy provides hundreds of built-in mathematical functions for algebra, statistics, trigonometry, logarithms, random number generation, matrix operations, and much more.
It integrates seamlessly with popular Python libraries such as Pandas, Matplotlib, SciPy, TensorFlow, PyTorch, and Scikit-learn.
Because NumPy is open source, it has a large community, excellent documentation, and continuous updates.
Installing NumPy Using pip
If Python is already installed on your computer, installing NumPy is very simple.
Open Command Prompt on Windows or Terminal on Linux/macOS.
Execute the following command.
pip install numpy
If your computer has multiple Python versions installed, use:
python -m pip install numpy
or
python3 -m pip install numpy
After installation, verify whether NumPy has been installed successfully.
pip show numpy
You can also verify the installation by checking the installed version inside Python.
import numpy as np print(np.__version__)
Example Output
2.3.2
The version number may differ depending on the latest release.
Installing NumPy Using Anaconda
If you are using Anaconda Distribution, NumPy is usually pre-installed because it is one of the core libraries included in Anaconda.
To install or update NumPy manually, open Anaconda Prompt and execute:
conda install numpy
To verify the installation:
conda list numpy
If NumPy already exists, Anaconda will simply display the installed version information.
Using NumPy in JupyterLab
JupyterLab is one of the best environments for learning Data Science because it allows you to write code, execute it one cell at a time, and immediately view the output.
To begin using NumPy in JupyterLab:
- Open Anaconda Navigator.
- Launch JupyterLab.
- Create a new Python Notebook.
- In the first cell, type:
import numpy as np print(np.__version__)
Run the cell.
Practice Assignment – NumPy Basics
Now it's your turn! Solve the following questions without looking at the answers first. These exercises will help you strengthen your understanding of NumPy arrays, installation, importing, and indexing.
Question 1
Write a Python program to import the NumPy library and display its installed version.
Answer
import numpy as np print(np.__version__)
Question 2
Create a NumPy array containing the following numbers:
10, 20, 30, 40, 50
Print the complete array.
Answer
import numpy as np numbers = np.array([10, 20, 30, 40, 50]) print(numbers)
Output
[10 20 30 40 50]
Question 3
Create a NumPy array of floating-point numbers.
2.5, 4.8, 6.3, 8.9
Print the array.
Answer
import numpy as np arr = np.array([2.5, 4.8, 6.3, 8.9]) print(arr)
Output
[2.5 4.8 6.3 8.9]
Question 4
Create a NumPy array containing the following programming languages:
Python NumPy Pandas AI
Print the array.
Answer
import numpy as np languages = np.array(["Python", "NumPy", "Pandas", "AI"]) print(languages)
Output
['Python' 'NumPy' 'Pandas' 'AI']
Question 5
Create the following array.
[100, 200, 300, 400, 500]
Print the:
- First element
- Third element
- Last element
Answer
import numpy as np
arr = np.array([100, 200, 300, 400, 500])
print("First Element:", arr[0])
print("Third Element:", arr[2])
print("Last Element:", arr[-1])
Output
First Element: 100 Third Element: 300 Last Element: 500
Question 6
Create an array containing the numbers from 1 to 5 and print each element individually using indexing.
Answer
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr[0]) print(arr[1]) print(arr[2]) print(arr[3]) print(arr[4])
Output
1 2 3 4 5
Question 7
Which statement correctly imports NumPy?
A. import NumPy
B. import numpy
C. import numpy as np
D. include numpy
Answer
✅ Correct Answer: C
import numpy as np
Question 8
Which command is used to install NumPy using pip?
A.
pip install numpy
B.
install numpy
C.
python install numpy
D.
numpy install
Answer
✅ Correct Answer
pip install numpy
Question 9
Which command is used to install NumPy using Anaconda?
A.
conda install numpy
B.
pip install numpy
C.
anaconda install numpy
D.
install numpy
Answer
✅ Correct Answer
conda install numpy
Question 10
What will be the output?
import numpy as np arr = np.array([5, 10, 15, 20, 25]) print(arr[-2])
Answer
20
Explanation:
Negative indexing starts from the end of the array.
Index : 0 1 2 3 4 Array : 5 10 15 20 25 Neg. Index : -5 -4 -3 -2 -1
Therefore, arr[-2] returns 20.
Mini Challenge (Without Looking at the Answers)
Write a Python program that:
- Imports NumPy.
- Creates an array of your five favorite numbers.
- Prints the complete array.
- Prints the first element.
- Prints the middle element.
- Prints the last element.
Expected Solution
import numpy as np
favorite_numbers = np.array([7, 14, 21, 28, 35])
print("Array:", favorite_numbers)
print("First:", favorite_numbers[0])
print("Middle:", favorite_numbers[2])
print("Last:", favorite_numbers[-1])
Interview & Viva Questions
- What does NumPy stand for?
- Why is NumPy faster than Python lists?
- What is an ndarray?
- Which command installs NumPy using pip?
- Which command installs NumPy using Anaconda?
- Why do we use import numpy as np?
- What is the difference between a Python list and a NumPy array?
- What is positive indexing?
- What is negative indexing?
- Name three real-world applications of NumPy.





