Python Foundation
Arrays in Python
Why Do We Need Arrays?
Imagine you want to store 5 employees' salaries:
Python
emp1 = 50000
emp2 = 60000
emp3 = 70000
emp4 = 30000
emp5 = 10000
But what if there are 1,000 employees?
Nobody wants to manage emp1, emp2, ... emp1000.
We need one variable capable of holding multiple related values.
What is an Array?
An array is a collection of multiple values stored under one name, where individual values can be accessed using their index position.
Python
import numpy as np
In Python, we use the NumPy library to work with arrays efficiently.
Creating Arrays
Python
# 1D Array
a = np.array([10, 20, 30, 40, 50])
print(a)
# 2D Array
b = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(b)
Important Array Attributes
Python
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape) # (rows, columns)
print(a.ndim) # number of dimensions
print(a.size) # total number of elements
print(a.dtype) # data type of elements
Indexing and Slicing
Python
a = np.array([10, 20, 30, 40, 50, 60])
print(a[0]) # first element
print(a[1:4]) # slicing
print(a[::2]) # every second element
print(a[::-1]) # reverse
Useful NumPy Functions
Python
print(np.zeros((2, 3))) # array of zeros
print(np.ones((2, 3))) # array of ones
print(np.arange(1, 10, 2)) # range with step
print(np.linspace(0, 1, 5)) # evenly spaced values
print(np.eye(3)) # identity matrix
Vectorized Operations
NumPy allows you to perform operations on entire arrays without writing loops.
Python
a = np.array([10, 20, 30, 40])
b = np.array([1, 2, 3, 4])
print(a + b)
print(a * 2)
print(a > 25)
Reshaping Arrays
Python
a = np.arange(12)
print(a)
b = a.reshape(3, 4)
print(b)
reshape() changes the shape without changing the data.
Broadcasting
Broadcasting allows NumPy to perform operations on arrays of different shapes.
Python
a = np.array([
[10, 20, 30],
[40, 50, 60]
])
b = np.array([1, 2, 3])
print(a + b)
Here
b (shape (3,)) is broadcasted across the rows of a (shape (2,3)).
Broadcasting Rules
NumPy compares shapes from the right side:
- Dimensions are compatible when they are equal, or one of them is 1.
Python
# Compatible example
a = np.array([[1, 2, 3], [4, 5, 6]]) # shape (2,3)
b = np.array([10, 20, 30]) # shape (3,)
print(a + b)
# Incompatible example
c = np.array([10, 20]) # shape (2,)
# a + c → ValueError
Very Important:
Shape
Shape
(3,) is different from shape (3,1).
Practical Broadcasting Examples
Python
marks = np.array([
[80, 70, 90],
[60, 75, 85],
[90, 88, 95]
])
grace = np.array([5, 2, 3])
print(marks + grace) # add grace marks to each subject
Python
# Adding different values to each row
a = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
b = np.array([[100],
[200],
[300]]) # shape (3,1)
print(a + b)
Summary
- Arrays store multiple values under one name.
- NumPy provides fast and efficient array operations.
- Important attributes:
shape,ndim,size,dtype. - Supports vectorized operations (no loops needed).
- Broadcasting allows operations between different shaped arrays.
- Shape
(3,)is different from(3,1)— this is very important.
Arrays in Python (NumPy)
Learn how to store and process multiple values efficiently using NumPy arrays.
Select any topic from the left sidebar to begin.
Start Learning →
