Python NumPy Tutorial: Learn Python Numpy With Examples
By Rohit Sharma
Updated on Aug 14, 2025 | 11 min read | 7.26K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Aug 14, 2025 | 11 min read | 7.26K+ views
Share:
Ever tried working with long lists of numbers in Python? It feels slow. What if there was a better, faster way? There is, and it’s called NumPy.
NumPy gives Python a superpower for handling numbers. It adds a special kind of list, called an array, that's incredibly fast for math. This is why data science and machine learning in Python all start with NumPy.
This guide is the perfect NumPy tutorial for anyone feeling lost. As a complete NumPy in Python tutorial, it will walk you through the absolute basics step-by-step. Think of it as your first, friendly NumPy tutorial to get you started on your data journey. Let's begin!
Learn data science online courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
NumPy stands for ‘Numerical Python.’ As you would’ve guessed, it focuses on numerical operations and computing. Numpy is a Python package and is the main library for scientific computations. It has n-dimensional array objects and tools to integrate other dominant languages such as C. You can use the NumPy array as an enormous multi-dimensional container for data.
One advantage of a NumPy tutorial is that it empowers learners to efficiently process and analyze large datasets. Choosing NumPy in Python tutorial has the following benefits:
The NumPy array is a fantastic n-dimensional array object. It has rows and columns, and you can use it to access the elements of a Python list. There are many operations you can perform on a NumPy array. We’ve discussed them later in the article, but before that, you must understand how to install NumPy in your system. Without installing it, you wouldn’t be able to use it.
You’ll have to go to the command prompt and enter ‘pip install numpy’ to install Python NumPy. After the installation is complete, you’ll have to go to the IDE and import numpy through ‘import numpy as np’. And that’s how you install Numpy on your system.
In the NumPy tutorial, arrays may be created in a variety of ways.
You can create arrays in NumPy easily through the following code:
import numpy as np
a=np.array([1,2,3])
print(a)
The output of the above code – [1 2 3]
The code above would give you a one-dimensional array. If you want to create a multidimensional array, you’d have to write something similar to the example present below:
a=np.array([(1,2,3),(4,5,6)])
print(a)
The output of the above code – [[ 1 2 3]
[4 5 6]]
Read more: Top 50 Python Project Ideas with Source Code in 2025
Understanding the fundamentals of array indexing is crucial for analysing and working with the array object. Numerous array indexing options are provided by NumPy.
In a Python NumPy tutorial, it’s essential to understand the distinctions between Python lists and NumPy arrays. With NumPy, you have a wide range of tools for quickly and effectively creating arrays and manipulating data. A Python list can include a variety of data types, while NumPy arrays can only contain elements of the same data type. The proposed mathematical procedures would be exceedingly inefficient if the arrays were not uniform.
NumPy arrays offer vectorized operations and are optimised for numerical calculations. Using vectorized operations, it is possible to do element-wise calculations on whole arrays without using explicit loops. As a result, NumPy arrays perform numerical operations much quicker than Python lists, greatly increasing computational efficiency.
Learn NumPy from scratch in this free, full-length 2025 tutorial for Python beginners.
Python NumPy has many operations. They all perform specific functions. Here are those functions with a brief description:
itemsize:
With the help of this function, you can find out the byte size of the elements of your array. Take a look at the following example:
import numpy as np
a = np.array([(1,2,3)])
print(a.itemsize)
The output of the above code – 4
Popular Data Science Programs
ndim:
The ndim function helps you find the dimension of the array. You should know that you can have one dimensional, two dimensional, as well as three-dimensional arrays. Here’s an example of this function:
import numpy as np
a = np.array([(1,2,3),(4,5,6)])
print(a.ndim)
The output of the above code – 2
reshape:
With the help of the reshape operation, you can change the number of rows and columns present in an array. Suppose the one array has three columns and two rows. Through reshape, you can change them to 2 columns and three rows. See it in action through the following example:
import numpy as np
a = np.array([(8,9,10),(11,12,13)])
print(a)
a=a.reshape(3,2)
print(a)
Output of the above code – [[ 8 9 10] [11 12 13]] [[ 8 9] [10 11] [12 13]]
slicing:
By using the slicing operation, you can extract a specific set of elements from the required array. In other words, you can ‘slice’ the array and get a portion of the same. Suppose you have an array and want to extract a specific element from it, you’d go about it in the following way:
import numpy as np
a=np.array([(1,2,3,4),(3,4,5,6)])
print(a[0,2])
The output of the above code – 3
In the example above, the index of the first array was 0, and for the second one, it was 1. So, the code says that it should print the second element of the first array (that has the index 0). Suppose you need the second element from the first and the zeroth index of the array. Then we would use the following code:
import numpy as np
a=np.array([(1,2,3,4),(3,4,5,6)])
print(a[0:,2])
The output of the above code– [3 5]
Also read: Python Developer Salary in India in 2025
dtype:
WIth the dtype function, you have the option of finding the data type of the elements of an array. It gives you the data type and the size of the required component. Take a look at the following example to see how it works:
import numpy as np
a = np.array([(1,2,3)])
print(a.dtype)
The output of the above code – int32
You can use the ‘shape’ and ‘size’ functions to find the shape and size of the array as well. Take a look at this example of our Python NumPy tutorial to understand these functions properly:
import numpy as np
a = np.array([(1,2,3,4,5,6)])
print(a.size)
print(a.shape)
The output of the above code – 6 (1,6)
linspace:
With the help of the linspace operation, you can get evenly spaced numbers spread according to your mentioned interval. The linspace function has its uses, and here’s an example of how you can use it:
import numpy as np
a=np.linspace(1,3,10)
print(a)
Output of the above code– [ 1. 1.22222222 1.44444444 1.66666667 1.88888889 2.11111111 2.33333333 2.55555556 2.77777778 3. ]
square root and standard deviation
Python NumPy enables you to perform various mathematical operations. And one of those operations is deriving the square root of the required array. You can also obtain the standard deviation of your NumPy array. Here’s a detailed example to help you in this regard:
import numpy as np
a=np.array([(1,2,3),(3,4,5,)])
print(np.sqrt(a))
print(np.std(a))
The output of the above code– [[ 1. 1.41421356 1.73205081]
[ 1.73205081 2. 2.23606798]]
1.29099444874
max/min
You can find the maximum, minimum, and the sum of an array as well through the specific operations. Finding the maximum and the minimum can help you a lot in performing complex operations. Here is how you can find the maximum, minimum, and the sum of the array you have:
import numpy as np
a= np.array([1,2,3])
print(a.min())
print(a.max())
print(a.sum())
The output of the above code – 1 3 6
Horizontal and vertical stacking
You might want to combine two arrays but not add them, i.e., you might just want to concatenate them. For that purpose, you can either stack them vertically or horizontally. Here is the example code for doing so:
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(np.vstack((x,y)))
print(np.hstack((x,y)))
Output of the above code – [[1 2 3] [3 4 5] [1 2 3] [3 4 5]]
[[1 2 3 1 2 3] [3 4 5 3 4 5]]
Read more: Operators in Python: A Beginner’s Guide to Arithmetic, Relational, Logical & More
Addition
You can add NumPy arrays as well. Apart from addition, you can also perform subtraction, division, and multiplication of two matrices. Here’s an example of addition in Python NumPy:
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(x+y)
The output of the above code – [[ 2 4 6] [ 6 8 10]]
Like we mentioned earlier, you can perform other mathematical operations on NumPy arrays as well, including subtraction and division. Here’s how:
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
y= np.array([(1,2,3),(3,4,5)])
print(x-y)
print(x*y)
print(x/y)
Output of the above code– [[0 0 0] [0 0 0]]
[[ 1 4 9] [ 9 16 25]]
[[ 1. 1. 1.] [ 1. 1. 1.]]
ravel
The ravel operation lets you convert a NumPy array into a ravel, which is a single column. Here’s an example:
import numpy as np
x= np.array([(1,2,3),(3,4,5)])
print(x.ravel())
The output of the code – [ 1 2 3 3 4 5]
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
upGrad’s Exclusive Data Science Webinar for you –
Watch our Webinar on How to Build Digital & Data Mindset?
Check out all trending Python tutorial concepts in 2024.
Mastering NumPy is a game-changer, and you're now well on your way. This NumPy tutorial has walked you through the essentials, from arrays to core operations, unlocking the library that powers all of data science.
The key is to keep applying what you've learned. This NumPy tutorial will always be here as a reference, so challenge yourself with new problems and put your powerful new skills to good use!
If you are curious to learn about data science, check out IIIT-B & upGrad’s Executive Diploma in Data Science & AI which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
NumPy is a widely used library for working with arrays in Python. There are certain functions in NumPy that can also allow you to work in the domain of matrices, Fourier transform, and linear algebra.
Lists are used in Python for serving the purpose of an array. The only downside here is that they are pretty slow to process. NumPy has the ability to provide an array object, which is found to be 50x faster as compared to the traditional Python lists. There are various supporting functions provided with the array object in NumPy in order to make its working much simpler and easier. Whenever it comes to speed and resources in data science, arrays are considered, and that’s when NumPy comes into play.
You can install NumPy using a package manager like pip with the command: pip install numpy. In your Python script, it's a universal convention to import it with the alias np using the line: import numpy as np.
NumPy array is known to be a faster alternative to the traditional Python lists. No matter what operation you wish to perform on the data, you will find that the NumPy array is much more accurate than a list.
As the size of the array increases, the speed of NumPy gets 30x faster as compared to the Python lists. So, even if you perform a simple delete operation, you'll notice that NumPy arrays are fast. As the NumPy arrays are densely packed because of its homogeneous type, it also tends to clear memory faster.
An ndarray (N-dimensional array) is the core object in NumPy. It's a grid of values of the same data type, indexed by a tuple of non-negative integers. It's what makes NumPy so powerful and fast
The three most critical attributes are: .shape (the dimensions of the array), .dtype (the data type of the elements, e.g., int64, float64), and .size (the total number of elements in the array).
np.arange() creates an array with values in a given range using a defined step size. np.linspace() creates an array with a specific number of values that are evenly spaced between a start and end point.
Operations like addition, subtraction, and multiplication are performed element-wise. This means the operation is applied to each element in the array individually without needing a loop, which is a key reason for NumPy's speed.
Broadcasting is a powerful mechanism that allows NumPy to perform operations on arrays of different shapes. The smaller array is "broadcast" across the larger array so that they have compatible shapes. For example, you can add a single number (a scalar) to every element in an array.
A ufunc, or Universal Function, is a function that operates on ndarrays in an element-by-element fashion. They are the "vectorized" wrappers for fast C-language functions, which is why NumPy's mathematical operations are so fast. Examples include np.add, np.sin, and np.exp.
Similar to Python lists, you can access elements using square brackets []. For multi-dimensional arrays, you use a comma-separated tuple of indices, like arr[row, column]. Slicing allows you to select a range of elements, like arr[1:5].
The .reshape() method allows you to change the shape (dimensions) of an array without changing its data. For example, you can turn a flat 1D array of 12 elements into a 3x4 2D array (a matrix).
Both are used to combine arrays. np.vstack() stacks arrays vertically (row-wise), increasing the number of rows. np.hstack() stacks arrays horizontally (column-wise), increasing the number of columns.
The axis parameter defines the dimension along which a function is applied. In a 2D array (a matrix), axis=0 refers to the vertical dimension (down the rows), and axis=1 refers to the horizontal dimension (across the columns). Mastering the axis parameter is a key milestone in any numpy tutorial.
A copy is a completely new array with its own data. A view is just a different way of looking at the same data. Modifying a view will change the original array, which is a critical concept to understand to avoid unintentional bugs. Slicing an array creates a view.
NumPy's random module is a powerful tool for this. You can generate arrays of random numbers from various distributions using functions like np.random.rand() (for uniform distribution) or np.random.randn() (for normal distribution).
Yes. While Pandas is more common for complex files like CSVs, NumPy has simple functions like np.loadtxt() and np.genfromtxt() that are very efficient for loading numerical data from text files.
Use NumPy for numerical computations, especially when working with homogeneous numerical data in multi-dimensional arrays (like images or matrices). Use Pandas for data analysis, especially with tabular, heterogeneous data (like a spreadsheet with different data types in columns) and when you need features like labels, indexing by name, and handling missing data.
NumPy is foundational because other major data science libraries are built on top of it. For example, Pandas DataFrames use NumPy arrays under the hood to store data and perform fast calculations. That's why any good numpy in python tutorial will emphasize that you must understand NumPy first before moving on to Pandas or Scikit-learn.
While free resources like the official documentation and online tutorials are useful for foundational knowledge, the most efficient and career-focused way to master NumPy is through a structured program.
A comprehensive course or progrm, such as the Masters in Data Science Degree offered by upGrad from LJMU, provides a clear curriculum designed by industry experts. This approach ensures you move beyond theory with hands-on projects, personalized mentorship, and a clear path to becoming proficient in NumPy and its real-world applications.
The most important habit is to think in terms of "vectorization"—applying operations to entire arrays instead of using Python for loops. Other key practices include pre-allocating arrays and being mindful of whether an operation creates a view or a copy. Learning these habits is the main goal of a great numpy tutorial in python.
834 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources