Python NumPy Tutorial: From Basics to Advanced

Python has become one of the most popular programming languages, especially in the fields of data science, artificial intelligence, and machine learning. One of the major reasons behind Python’s popularity is its powerful libraries. Among these, Python NumPy Tutorial (Numerical Python) is one of the most essential libraries used for scientific computing. If you want to work with large datasets, perform mathematical operations, or build data-driven applications, mastering NumPy is a must.

In this tutorial, we’ll explore NumPy step by step — from the basics to advanced concepts — with examples to make learning easier.

What is NumPy?

NumPy is a Python library that provides support for:

  • Multi-dimensional arrays and matrices

  • Mathematical functions to operate on these arrays

  • High performance in numerical computations

Instead of relying on Python’s built-in lists, NumPy uses arrays that are more efficient, consume less memory, and are faster when performing complex calculations.

Installing NumPy

To install NumPy, you can use pip:

pip install numpy

Once installed, you can import it in your Python script:

import numpy as np

Here, np is the common alias used by developers.

NumPy Basics

1. Creating Arrays

import numpy as np  

# Creating 1D array
arr1 = np.array([1, 2, 3, 4])  

# Creating 2D array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])  

print(arr1)  
print(arr2)  

NumPy arrays can be one-dimensional or multi-dimensional.

2. Array Attributes

print(arr2.shape)   # Shape of the array
print(arr2.ndim)    # Number of dimensions
print(arr2.size)    # Total number of elements
print(arr2.dtype)   # Data type of elements

3. Creating Special Arrays

zeros = np.zeros((2, 3))  
ones = np.ones((3, 3))  
range_arr = np.arange(0, 10, 2)  
linspace_arr = np.linspace(0, 1, 5)  

print(zeros)  
print(ones)  
print(range_arr)  
print(linspace_arr)  

Array Operations

One of the biggest advantages of NumPy is its ability to perform operations efficiently.

a = np.array([10, 20, 30, 40])  
b = np.array([1, 2, 3, 4])  

print(a + b)   # Element-wise addition  
print(a - b)   # Element-wise subtraction  
print(a * b)   # Element-wise multiplication  
print(a / b)   # Element-wise division  

NumPy also supports mathematical functions:

print(np.sqrt(a))  
print(np.mean(a))  
print(np.max(a))  
print(np.min(a))  

Indexing and Slicing

You can access array elements like lists:

arr = np.array([10, 20, 30, 40, 50])  
print(arr[0])    # First element  
print(arr[-1])   # Last element  

print(arr[1:4])  # Slice from index 1 to 3  

For 2D arrays:

matrix = np.array([[1, 2, 3], [4, 5, 6]])  
print(matrix[0, 1])  # Element at row 0, column 1  
print(matrix[:, 1])  # Entire second column  
print(matrix[1, :])  # Entire second row  

Advanced NumPy Features

1. Broadcasting

Broadcasting allows operations between arrays of different shapes.

a = np.array([1, 2, 3])  
b = 2  
print(a * b)  # [2, 4, 6]  

2. Reshaping

arr = np.arange(1, 13)  
reshaped = arr.reshape(3, 4)  
print(reshaped)  

3. Stacking and Splitting

x = np.array([[1, 2], [3, 4]])  
y = np.array([[5, 6], [7, 8]])  

print(np.hstack((x, y)))  
print(np.vstack((x, y)))  

4. Random Numbers

NumPy provides tools for generating random numbers:

rand_arr = np.random.rand(3, 3)  
rand_int = np.random.randint(1, 10, 5)  

print(rand_arr)  
print(rand_int)  

Why NumPy is Important?

  • Speed: NumPy arrays are much faster than Python lists.

  • Memory Efficient: Uses less memory than traditional lists.

  • Foundation for Data Science: Libraries like Pandas, Scikit-learn, and TensorFlow are built on NumPy.

  • Mathematical Functions: Built-in functions for linear algebra, statistics, Fourier transforms, and more.

Conclusion

Python NumPy Tutorial is the backbone of numerical and scientific computing in Python. From simple array creation to complex matrix manipulations, it offers powerful tools for every data scientist and programmer. Whether you are just starting with Python or preparing for advanced machine learning projects, mastering NumPy is an essential step.

📍 Contact Info
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India 
hr@tpointtech.com
📞 +91-9599086977




Comments

Popular posts from this blog

Quantitative Aptitude Questions and Answers with Solutions for Beginners

Java Tutorial: Master Object-Oriented Programming

Exception Handling in Java: Try, Catch, and Throw