Beginner Python Exercises

Factorial in Python with a Loop

6! = 720. Multiply your way there with a loop, then compare with math.factorial.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • The definition of n!
  • Multiplying in a loop
  • Rejecting negative input
result after each step for 6!
1start
2× 2
6× 3
24× 4
120× 5
720× 6

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.

From lining up people to code
result = 1🧮 Start with one way (nobody in line)
for value in range(2, number + 1):👥 Add people one by one
result *= value✖️ Multiply by the choices for the next place
raise ValueError(...)🚫 Negative numbers of people make no sense
6! = 6 × 5 × 4 × 3 × 2 × 1 = 720 ways.
In one sentence

The 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

  1. raise ValueError(...) rejects negative numbers, for which a factorial is not defined.
  2. result = 1 is the starting value, which also makes factorial(0) and factorial(1) return 1.
  3. for value in range(2, number + 1): goes through 2, 3, ... up to the number itself.
  4. result *= value multiplies 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.factorial is 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

Run code →
main.py
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))
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Starting the product at 0

Anything multiplied by 0 stays 0.

Problem
result = 0
for value in range(2, number + 1):
    result *= value
Fix
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.

Problem
for value in range(2, number):
Fix
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.

All challenges