Factorial in Python with a Loop
6! = 720. Multiply your way there with a loop, then compare with math.factorial.
- The definition of n!
- Multiplying in a loop
- Rejecting negative input
Multiply 1 × 2 × ... × n
The problem
Counting arrangements needs factorials: how many ways can 6 runners finish a race, or 10 books stand on a shelf? The numbers grow very quickly, and a loop that multiplies step by step is the clearest way to compute them.
Think of it like this
A factorial is like counting ways to line up people. The first place can go to any of 6 people, the second to any of the 5 left, then 4, 3, 2 and 1. Multiplying those choices together gives every possible order.
result = 1🧮 Start with one way (nobody in line)for value in range(2, number + 1):👥 Add people one by oneresult *= value✖️ Multiply by the choices for the next placeraise ValueError(...)🚫 Negative numbers of people make no senseThe factorial of a non-negative whole number n, written n!, is the product of all whole numbers from 1 to n, and 0! is 1.
Key points
raise ValueError(...)rejects negative numbers, for which a factorial is not defined.result = 1is the starting value, which also makesfactorial(0)andfactorial(1)return 1.for value in range(2, number + 1):goes through 2, 3, ... up to the number itself.result *= valuemultiplies the running product;factorial(6)returns 720.
When should you use it?
- Counting arrangements and combinations in probability exercises.
- Practicing loops that build up a product.
- Comparing a loop with a recursive solution of the same problem.
When not to use it
- Real projects:
math.factorialis built in, tested and fast. - Very deep recursion: a recursive factorial of 5,000 hits Python's recursion limit; the loop does not.
Example code
def factorial(number):
if number < 0:
raise ValueError("Factorial is undefined for negative numbers")
result = 1
for value in range(2, number + 1):
result *= value
return result
print(factorial(6))
Result of running the code
720
Code explanation
Starting with 1 preserves the correct value for 0 factorial. The loop multiplies the result by each integer through n.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Starting the product at 0
Anything multiplied by 0 stays 0.
result = 0
for value in range(2, number + 1):
result *= value
result = 1
for value in range(2, number + 1):
result *= value
Stopping one number too early
range stops before its end value, so the number itself is never multiplied.
for value in range(2, number):
for value in range(2, number + 1):
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Factorial Multiply all positive integers up to n. Easy
- Power by Repeated Multiplication Raise a number to a power with a loop. Easy