Python Elementwise Operations
a + b Practice

Pair each index: a[i] with b[i]

a + b adds matching positions. Same length (or broadcastable shapes) required.

Picture this: pair cells in lockstep

a + b adds matching positions. No Python for-loop needed.

a[i]left cell
→
b[i]right cell
→
a[i]+b[i]same index
→
new arrayresult

In easy words

Two arrays line up by index. Position 0 with 0, 1 with 1, and so on. The result is a new array of those pairwise results.

Where this shows up

Adding bias vectors, combining RGB channels, residual connections in ML, physics field updates.

Key facts

  • Elementwise — + - * / apply per paired index.
  • Shape match — Lengths must agree (or broadcast).
  • Not matrix multiply — Use @ or np.dot for that.
  • New array — Result is usually a new ndarray.

What to remember

  • Example — [1,2,3] + [4,5,6] → [5,7,9].
  • Visual check — Align columns before coding.
  • Scalar mix — arr + 10 adds 10 to every cell.
  • Errors — ValueError on incompatible shapes.
Interactive visualizer

Pairwise add

Press Add to compute a + b

Example Program

1import numpy as np
2a = np.array([1, 2, 3])
3b = np.array([4, 5, 6])
4print(a + b) # [5 7 9]