Arrays - operations

NumPy - The Basics

2 min read

Published Sep 22 2025, updated Aug 17 2026


11
0
0
0

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 arraysa = 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]

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:
    1. If arrays have different dimensions, prepend 1s to the smaller shape.
    2. Arrays are compatible if in every dimension they are equal or one of them is 1.
    3. NumPy stretches the dimension with size 1 to match the other array.

Examples:

import numpy as np# 1D arraya = 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 operationsprint("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.]]




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# Arraysa = np.array([1, 2, 3, 4])b = np.array([2, 2, 0, 5])# Element-wise comparisonprint("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 scalarprint("a > 2:", a > 2)       # → [False False  True  True]print("a <= 3:", a <= 3)     # → [ True  True  True False]

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]
© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
NumPy - The Basics | Arrays - operations | SimpleSteps.guide