Module 4 · Creating multi-parameter functions · Lesson 3 of 5
Factorials
The factorial of a number n (written n!) is every whole number from 1 up to n, all multiplied together. So 4! = 1 · 2 · 3 · 4 = 24. It's a perfect fit for the loop-and-accumulate pattern.
factorial(4) = 1·2·3·41×2×3×4
result =1
The idea: start a result at 1, then loop through the numbers, multiplying each one into it. The running total grows step by step — 1 → 2 → 6 → 24 — and the final value is the answer you return.
Run it yourselfruns in your browser · Python
main.py
Starting result at 1 (not 0) is the trick — and it neatly makes factorial(0) come out as 1, just as maths says it should. Next, a sequence built the same loop-driven way: Fibonacci.