Monday, October 19, 2026

Python NumPy: From Beginner to Advanced - Part 1

Numpy is a python library which is known for Numerical Python. It provides powerful datastructure called ndarray (N-dimensional Array). 

For example:

We have a array

numbers = [1, 5, 3, 6, 8]

To use Numpy, first import it.

import numpy as np
numbers = [1, 5, 3, 6, 8]
num = np.array[numbers]
print(num)

The alias np is the conventional short name for NumPy.

Why use Numpy?

Looping through arrays can be challenging for programmers. However, with NumPy, we can perform various types of calculations on entire arrays without using explicit loops. This is called vectorization.

For Example:

import numpy as np
number = [1, 2, 3, 4, 5]
addition = np.array(number)
print(addition+ 5)
Without defining a separate list, we can also write:
addition = np.array([1, 2, 3, 4, 5])
print(addition+ 5)

This example helps you understand that NumPy allows us to add 5 to each element in an array and print the result as an array.

More examples:

print(addition * 2) # Multiply each element by 2

print(addition ** 2) # Square each element

Read: Python Exercises for Beginners

How to Install Numpy

If you have already installed Python, open Command Prompt and run the following command to install NumPy:

python -m pip install numpy

Check NumPy Installation and Version

After the installation is complete, open Python and run:

import numpy as np
print(np.__version__)

If NumPy is installed successfully, you will see a version number, such as:

2.3.4

This confirms that NumPy has been successfully installed and is ready to use in your Python programs.

Numpy Array

NumPy uses one-dimensional, two-dimensional, and multidimensional arrays to store, organize, and perform calculations on multiple elements.

One Dimensional Array

A single list is use as one dimensional array.

number = [5, 10, 15, 20]

Two Dimensional Array

It contains elements arranged in rows and columns. It can be represented like a table or matrix.

number [[5, 10, 15], [20, 25, 30]]

Multi-DimensionalArray

It is commonly used to store data in multiple layers like collection of 2D arrays.

import numpy as np
  numbers = np.array([
[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]
]])
print(numbers)

Note: Array indexing start from 0.

Max(), Min() and Mean()

These function widely use get fast result without multiple line code. max (): Maximum, min(): Minimum, and mean(): Mean().

import numpy as np
numbers = np.array([43, 52, 23, 44, 15])
print(numbers.mean())
print(numbers.max())
print(numbers.min())

Similarly we can calcuate: sum(), substract(), divide(), and multiply().

print("Total:", numbers.sum())
print("Substract:", np.substract(50, 14)) # value given manually,Not from Array
print("Multiply:", np.multiply(55, 5)) # value given manually, Not from Array
print("Divide:", numbers/5)

Indexing and Slicing

Array indexing starts with zero (0), which means the first element has index 0, the second has index 1, and so on. While slicing allows us to extract a part of an Array.
import numpy as np
numbers = np.array([21, 23, 45, 63, 34, 67])
print(numbers[2])
print(numbers[1:4])

In slicing [1:4] = [start, stop] - i.e start from 1st index and stop before 4th index.

More example of slicing
print(numbers[:2])    #first 2 elements
print(numbers[2:])    #from index 2 to last
print(numbers[-2])    #second last element
print(numbers[::2])    #every second element

Shape, Size and ndim

This is widely use in data analysis and they are properties not function.
shape - extract total element and row and column 
size - total elements
ndim - dimension
import numpy as np

numbers= np.array([2,4,6,3,7])

print(numbers.shape)
print(numbers.size)
print(numbers.ndim)
Note: shape output changed according to the dimension and number of element in each dimension.

More example:

Two demension:
import numpy as np

numbers= np.array([[2,4,3],[5, 6, 7]])

print(numbers.shape)    # output: (2, 3) - 2 row, 3 column
print(numbers.size)    #output: 6 - 2 x 3
print(numbers.ndim)    #output: 2

Three Dimension

import numpy as np

numbers= np.array([
[[3,7],[4,2]],
[[8,1],[4,3]]
])

print(numbers.shape)    #output(2, 2, 3) - 2 block, 2 row and 3 column
print(numbers.size)    #output: 12 - 2 x 2 x 3
print(numbers.ndim)    #output: 3
Now practice 2D and 3D array so you can proficient in it.

Reshape ()

Using reshape(), we can change the structure of an Array without changing in value.
import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60])

print(numbers.shape) new_data = numbers.reshape(2, 3) print(new_data) print(new_data.shape)
Important point:
Output: 
6
[10, 20, 30,], [40, 50, 60]
6

You can see the shape value of the previous and after is 6. So whenever you reshape the array structure then the total number of value must be the same otherwise you will get an error.

No comments:

Post a Comment

Featured Post

Python OOPs Advanced: Class Variables and Instance Variables

We have learned how to create classes and objects . Now, we will explore class variables and instance variables in Python, including how the...