Arrays - operations
NumPy - The Basics
2 min read
This section is 2 min read, full guide is 14 min read
Published Sep 22 2025
11
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
NumPyPython
Arithmetic Operations
+,-,*,/,**→ element-wise addition, subtraction, multiplication, division, power.- Works element-wise, so array shapes must be compatible.
Examples:
import numpy as np
# Create arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + 5)
# [6 7 8]
print(a * 5)
# [ 5 10 15]
print("Addition:", a + b)
# Addition: [5 7 9]
print("Subtraction:", a - b)
# Subtraction: [-3 -3 -3]
print("Multiplication:", a * b)
# Multiplication: [ 4 10 18]
print("Division:", b / a)
# Division: [4. 2.5 2. ]
print("Power:", a ** 2)
# Power: [1 4 9]
Copy to Clipboard
Toggle show comments
Arithmetic operations also support broadcasting:
- Broadcasting allows NumPy to perform element-wise operations on arrays of different shapes.
- NumPy “stretches” the smaller array across the larger one without actually copying data.
- Rules for broadcasting:
- If arrays have different dimensions, prepend 1s to the smaller shape.
- Arrays are compatible if in every dimension they are equal or one of them is 1.
- NumPy stretches the dimension with size 1 to match the other array.
Examples:
import numpy as np
# 1D array
a = np.array([1, 2, 3])
# 2D array (3 rows, 3 columns)
b = np.array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
# Broadcasting in arithmetic operations
print("Addition:\n", b + a)
# → [[11 22 33]
# [41 52 63]
# [71 82 93]]
print("Subtraction:\n", b - a)
# → [[ 9 18 27]
# [39 48 57]
# [69 78 87]]
print("Multiplication:\n", b * a)
# → [[10 40 90]
# [40 100 180]
# [70 160 270]]
print("Division:\n", b / a)
# → [[10. 10. 10.]
# [40. 25. 20.]
# [70. 40. 30.]]
Copy to Clipboard
Toggle show comments
Comparison Operations
- Comparison operations are element-wise checks between arrays, or between an array and a scalar.
- They return a boolean array (
True/False) with the same shape as the input arrays which can be used for masking, filtering, or conditional operations. - Works for arrays of same shape, or using broadcasting if shapes are compatible.
Comparison operators:
==- Equal to!=- Not equal to<- Less than>- Greater than<=- Less than or equal to>=- Greater than or equal to
Examples:
import numpy as np
# Arrays
a = np.array([1, 2, 3, 4])
b = np.array([2, 2, 0, 5])
# Element-wise comparison
print("a == b:", a == b)
# → [False True False False]
print("a != b:", a != b)
# → [ True False True True]
print("a > b:", a > b)
# → [False False True False]
print("a < b:", a < b)
# → [ True False False True]
print("a >= b:", a >= b)
# → [False True True False]
print("a <= b:", a <= b)
# → [ True True False True]
# Comparison with scalar
print("a > 2:", a > 2)
# → [False False True True]
print("a <= 3:", a <= 3)
# → [ True True True False]
Copy to Clipboard
Toggle show comments
Combining Comparisons:
- You can combine comparisons using bitwise operators:
&- AND|- OR~- NOT
- Use parentheses for each condition when combining.
Example:
mask = (a > 1) & (b < 5)
print(mask)
# → [False False True True]
Copy to Clipboard
Toggle show comments














