Arrays - operations

NumPy - The Basics

2 min read

Published Sep 22 2025


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

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 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.]]




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]

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]

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact