Python Foundation

Arrays in Python

01 · Why Arrays? File · Arrays · 1 of 12

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.

02 · What is an Array? File · Arrays · 2 of 12

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.
03 · Creating Arrays File · Arrays · 3 of 12

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)
04 · Array Attributes File · Arrays · 4 of 12

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
05 · Indexing & Slicing File · Arrays · 5 of 12

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
06 · Useful Functions File · Arrays · 6 of 12

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
07 · Vectorized Operations File · Arrays · 7 of 12

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)
08 · Reshaping File · Arrays · 8 of 12

Reshaping Arrays

Python
a = np.arange(12)
print(a)

b = a.reshape(3, 4)
print(b)
reshape() changes the shape without changing the data.
09 · Broadcasting File · Arrays · 9 of 12

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)).
10 · Broadcasting Rules File · Arrays · 10 of 12

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 (3,) is different from shape (3,1).
11 · Practical Examples File · Arrays · 11 of 12

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)
12 · Summary File · Arrays · 12 of 12

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.
Home Arrays in Python

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 →