Factorial program in Python
Find the factorial of a number in Python with a for loop, recursion and math.factorial, including 0 and negative input. Run every example in your browser.
Short answer
In real code, use math.factorial(n). To write it yourself, multiply 1 × 2 × … × n in a for loop, or use recursion: return 1 if n <= 1 else n * factorial(n - 1). 0! is 1, and negative numbers have no factorial.
Method 1: a for loop
Start at 1 and multiply by every number up to n. range(2, n + 1) stops before its end, so the + 1 is what includes n itself. For 0 and 1 the loop does not run and the answer stays 1.
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
return result
print(factorial(5))
print(factorial(0))
120 1
Method 2: recursion
n! is n × (n − 1)!, so the function can call itself on a smaller number. The base case, n <= 1, stops the calls. Without it the function would recurse forever.
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
print(factorial(5))
print(factorial(10))
120 3628800
Method 3: math.factorial
The standard library already has it. It is written in C, checks its input, and handles very large numbers, because Python integers have no size limit.
import math
print(math.factorial(5))
print(math.factorial(20))
120 2432902008176640000
Method 4: read the number from the user
A complete program for school assignments: read n, reject negative numbers, then print the result. Type a number in the Input box under the editor before you press Run.
n = int(input("Enter a number: "))
if n < 0:
print("Factorial is not defined for negative numbers")
else:
result = 1
for i in range(2, n + 1):
result *= i
print(f"{n}! = {result}")
Enter a number: 5 5! = 120
Common mistakes
- Writing
range(1, n). It stops at n − 1, sofactorial(5)comes out as 24. Userange(2, n + 1)orrange(1, n + 1). - Starting with
result = 0. Anything times 0 is 0. A product starts at 1. - Recursing on big numbers. Python stops at about 1,000 nested calls with
RecursionError, so a recursivefactorial(5000)fails. Use the loop ormath.factorialfor large n.
Practice: your turn
Write solve(n) that returns n! using recursion. solve(0) and solve(1) return 1.
This challenge and its tests come from the PyMaster course. Press Run tests to check your function.
Input for input()
Runs real Python 3.14 in your browser with Pyodide. Your code is not uploaded. Ctrl + Enter runs the tests. Press Esc, then Tab, to leave the editor.
Hint 1
What is the smallest factorial you can answer directly?
Hint 2
If n <= 1, return 1; otherwise return n * solve(n - 1).
Hint 3
return 1 if n <= 1 else n * solve(n - 1)
Show a solution
def solve(n):
if n <= 1:
return 1
return n * solve(n - 1)
Questions
Why is 0! equal to 1?
It is the empty product: multiplying no numbers gives 1, the same way adding no numbers gives 0. It also keeps formulas such as n! = n × (n − 1)! true for n = 1.
What is the factorial of a negative number?
It is not defined. math.factorial(-1) raises ValueError, so check for negative input before you compute.
Loop or recursion: which is better?
The loop. It is faster and cannot hit the recursion limit. Recursion is worth learning because it is a common interview and exam question.
How big can a factorial get in Python?
As big as memory allows. Python integers grow as needed, so math.factorial(1000) works and has 2,568 digits.
