The factorial of a non-negative number n, written n!, is the product of all whole numbers from 1 to n. For example, 5! = 5 × 4 × 3 × 2 × 1 = 120. By definition, 0! = 1. Factorial is not defined for negative numbers. In this lesson you will find the factorial three ways: with a loop, with recursion, and with the math module.

Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.

 

Method 1: using a for loop

def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print(factorial(5))
print(factorial(0))
print(factorial(10))
print(factorial(20))

Output:

120
1
3628800
2432902008176640000

The variable result starts at 1. The loop multiplies it by 1, 2, 3 and so on up to n. For n = 0, the loop does not run, so the function returns 1, which is correct. Python integers can grow as large as memory allows, so even factorial(20) is exact.

 

Method 2: using recursion

A recursive function calls itself with a smaller value until it reaches a base case.

def factorial_recursive(n):
    if n == 0 or n == 1:
        return 1
    return n * factorial_recursive(n - 1)

print(factorial_recursive(5))

Output:

120

Here is what happens for n = 3:

factorial_recursive(3) = 3 * factorial_recursive(2)
factorial_recursive(2) = 2 * factorial_recursive(1)
factorial_recursive(1) = 1
so the result is 3 * 2 * 1 = 6

The base case (n is 0 or 1) stops the calls. Without it, the function would call itself forever.

 

Method 3: using the math module

import math

print(math.factorial(5))
print(math.factorial(20))

Output:

120
2432902008176640000

This is the shortest way, and it is usually the best choice in real programs.

 

Handle negative numbers

Factorial does not exist for negative numbers, so check for them first:

def factorial(n):
    if n < 0:
        return 'Factorial is not defined for negative numbers'
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

print(factorial(5))
print(factorial(0))
print(factorial(-3))

Output:

120
1
Factorial is not defined for negative numbers

Which method should you use?

Method

Good for

Watch out for

for loop

Understanding the logic, fast and simple

Nothing major

Recursion

Learning how recursion works

Python limits recursion depth (about 1,000 calls by default), so very large n can fail

math.factorial()

Real programs

You must import math

 

Take the number from the user (on your computer)

n = int(input('Enter a number: '))
print('Factorial of', n, 'is', factorial(n))

Our online compiler does not support input() yet, so use a fixed value there.

 

Common mistakes

  • Starting result at 0. Anything multiplied by 0 is 0, so result must start at 1.
  • Forgetting the base case in recursion. The function never stops and Python raises a RecursionError.
  • Using range(1, n). The stop value is not included, so use range(1, n + 1).

 

Practice questions

  1. Find the factorial of 7 using a loop.
  2. Print the factorials of all numbers from 1 to 10.
  3. Write a recursive function for the sum of the first n numbers.
  4. Calculate how many ways 5 people can stand in a line (the answer is 5!).

 

FAQs

What is the factorial of 0?

0! is 1 by definition.

Can I find the factorial of a negative number?

No. Factorial is defined only for zero and positive whole numbers.

Why is the answer so big for small numbers?

Factorials grow very fast. 20! is already more than two quintillion.