Cumulative & Difference Functions
Cumulative and difference functions turn an array of values into running totals or step-to-step changes, letting you compute rolling balances with cumsum and successive differences with diff in a single call.
Learn Cumulative & Difference Functions in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll build running totals with cumsum, running products with cumprod, neighbour differences with diff (including along an axis), and a smoother rate of change with gradient.
What You'll Learn in This Lesson
1 Running Totals with cumsum & cumprod
Where np.sum collapses to one number, np.cumsum keeps the length and stores the running total at each step. np.cumprod does the same with multiplication, giving a running product — handy for cumulative growth factors.
2 Step-to-Step Changes with np.diff
np.diff is the inverse idea: it returns arr[i+1] - arr[i] for each neighbouring pair, turning a series of values into the changes between them. Because each output needs two inputs, the result is one element shorter .
3 Along an Axis & np.gradient
On 2D arrays, both cumsum and diff take an axis argument so you can accumulate down columns or across rows. For a smooth rate of change that keeps the original length, reach for np.gradient , which uses central differences in the middle and one-sided at the edges.
🎯 YOUR TURN: Fill in the Blank
Replace ___ with the function that turns daily deposits into a running account balance.
Answer: cumsum — it accumulates the deposits into a running balance.
! Common Errors (And How to Fix Them)
✅ Fix: use np.diff(values, prepend=values[0]) to keep the original length, or slice days[1:] .
✅ Fix: use np.cumsum(arr) to keep the value at every position.
Without axis, cumsum and diff flatten the whole array first.
✅ Fix: pass axis=0 (down columns) or axis=1 (across rows) explicitly.
🎯 Mini Challenge: Sales Momentum
From weekly sales, compute the cumulative total and the week-over-week change, then report the best single-week jump.
❓ Frequently Asked Questions
Lesson complete — totals and changes unlocked!
You can build running totals with cumsum , running products with cumprod , step changes with diff (mind the shorter length), and smooth rates with np.gradient .
🚀 Up next: Checkpoint — Array Operations — put every skill from this section together.
Practice quiz
What does np.cumsum([3, 1, 2]) return?
cumsum keeps the running total at each step: 3, 3+1, 3+1+2.
What does np.cumprod([1, 2, 3, 4]) return?
cumprod gives the running product: 1, 1*2, 1*2*3, 1*2*3*4.
What does np.diff([10, 13, 9]) return?
diff returns each arr[i+1] - arr[i], so 13-10=3 and 9-13=-4.
How long is np.diff applied to an array of length 4?
- 4
- 5
- 3
- 2
Answer: 3. Each output needs two neighbours, so n inputs give n-1 differences.
The last element of np.cumsum(arr) always equals which value?
- arr.sum()
- arr.max()
- arr.mean()
Answer: arr.sum(). The final running total is the total of every element, i.e. arr.sum().
Which function gives a smooth rate of change that keeps the original length?
- np.diff
- np.cumsum
- np.gradient
- np.sum
Answer: np.gradient. np.gradient uses central differences and returns an array the same length as the input.
What does np.gradient(np.array([1., 4., 9., 16.])) return?
gradient uses one-sided differences at the ends and central differences inside.
On a 2D array, which argument makes cumsum accumulate across each row?
- axis=1
- axis=0
- axis=-1 only
- order='C'
Answer: axis=1. axis=1 runs along the columns within each row; axis=0 goes down rows.
How do you keep np.diff the same length as the input?
- Pass n=0
- Use prepend= a starting value
- Use np.sum instead
- It is impossible
Answer: Use prepend= a starting value. prepend= adds a leading value so the result matches the original length.
What does np.diff([20, 23, 19, 25], n=2) return?
n=2 differences the differences: diff([3,-4,6]) = [-7, 10].
Continue this course
- Previous: Rounding & Clipping
- Next: Checkpoint: Array Operations