NumPy, short for Numerical Python, is a powerful library in Python used for numerical computations. It provides support for large, multi-dimensional arrays and ...

NumPy, short for Numerical Python, is a powerful library in Python used for numerical computations. It provides support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays. In this Python Numpy tutorial, we'll explore the core functionalities of NumPy and learn how to work with arrays, perform mathematical operations, and vectorize your code for faster computations.

Before we get started, ensure you have NumPy installed. You can install it using pip:

pip install numpy
Now, let's import NumPy in our Python script:
import numpy as np

NumPy arrays are similar to Python lists, but they are more efficient and support various data types. Here's how you can create NumPy arrays:
arr = np.array([1, 2, 3])arr = np.zeros(5) or arr = np.ones(5)arr = np.array([1, 2, 3, 4, 5])
| Attribute/Method | Description |
|---|---|
shape |
Returns a tuple representing the dimensions of the array. |
ndim |
Returns the number of dimensions in the array. |
size |
Returns the total number of elements in the array. |
reshape |
Returns a new array with the specified shape. |
transpose |
Returns a new array with the rows and columns swapped. |
NumPy allows you to perform mathematical operations on entire arrays at once. Here are some examples:

arr1 + arr2arr1 * arr2arr1 / arr2np.sqrt(arr)



















Vectorization is a technique in NumPy where you perform operations on entire arrays instead of using loops. This results in faster computations. Here's an example of vectorization:
# Without vectorization
arr = np.array([1, 2, 3, 4, 5])
result = []
for i in arr:
result.append(i * 2)
# With vectorization
arr = np.array([1, 2, 3, 4, 5])
result = arr * 2
In this Python Numpy tutorial, we've covered the basics of NumPy arrays, their attributes, mathematical operations, and vectorization. By mastering these concepts, you'll be well on your way to writing efficient numerical computations in Python.