Python math.factorial() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.factorial(n) returns n! (the product 1 * 2 * ... * n) as an exact int for any non-negative integer n, with 0! == 1.

Negative numbers raise ValueError, and floats such as 2.0 raise TypeError since Python 3.10 (Python 3.9 deprecated accepting integral floats). For continuous real inputs, use math.gamma(x + 1).

math.factorial() is a function in Python's standard math module: import it with import math and call math.factorial(...), or import the name directly with from math import factorial.

Syntax

Import math and pass a non-negative integer.

python
import math

n_fact = math.factorial(n)
CallWhat it does
math.factorial(0)Returns 1.
math.factorial(5)Returns 120.
math.factorial(-1)Raises ValueError: negative input.
math.factorial(2.0)Raises TypeError (on Python 3.10+; deprecated in 3.9).

Use math.comb() or math.perm() directly instead of computing huge factorials and dividing them, since they cancel factors before multiplying.

Function signatures
python
math.factorial(n, /)
# returns: exact int n! for int n >= 0

Parameters

ParameterDescription
nRequired positional non-negative integer (int).

Return Value

Return ValueDescription
intExact factorial of n.

Python Examples

Example 1 — Print Exact Factorials Up to 20! Without Overflow

Watch how rapidly math.factorial() grows while staying an exact Python integer.

python
import math

for n in [0, 1, 5, 10, 20]:
    fact = math.factorial(n)
    print(f"{n:>2}! = {fact}")

How It Works

  1. Empty product is one. By mathematical convention, 0! equals 1.
  2. Rapid combinatorial growth. Moving from 5! (120) to 10! (3.6 million) and 20! (2.43 quintillion) outgrows 64-bit floats.
  3. Exact arbitrary precision. All 19 digits of 20! are exact integers with zero rounding.

Example 2 — Count Seatings and Cancel Adjacent Factorials

Count total orderings and verify 10! // 7! matches math.perm(10, 3).

python
import math

print("4 books:", math.factorial(4))
print("6 racers:", math.factorial(6))
ratio = math.factorial(10) // math.factorial(7)
print("10! // 7!:", ratio, ratio == math.perm(10, 3))

How It Works

  1. Full permutations. Four distinct books can be arranged in 24 ways and six runners in 720 orders.
  2. Exact integer division. Dividing 10! by 7! with // equals 10 * 9 * 8 = 720.
  3. perm() shortcut. math.perm() computes partial permutations like math.perm(10, 3) without building the full 10!.

Example 3 — Handle Negative Values and Non-Integer Arguments

Catch ValueError for negative ints and TypeError for fractional floats.

python
import math

try:
    math.factorial(-3)
except ValueError:
    print("negative factorial raises ValueError")

try:
    math.factorial(2.5)
except TypeError:
    print("non-integer factorial raises TypeError")

How It Works

  1. Negative domain error. Passing -3 raises ValueError immediately.
  2. Fractional float rejected. Passing 2.5 raises TypeError; use math.gamma(3.5) for real extensions.
  3. Version note for 2.0. Since Python 3.10, even whole-valued floats like 2.0 raise TypeError.

Frequently Asked Questions

1, matching the empty-product definition in mathematics.
Passing integral floats like 2.0 was deprecated in Python 3.9 and removed in Python 3.10, so factorial() now requires an integer (or object with __index__()).
No. Python integers have arbitrary precision, so factorial(100) computes all 158 digits accurately.
Use math.gamma(x + 1), because gamma(n + 1) equals n! for integers and generalizes to real floats.
No, prefer math.comb(n, k) or math.perm(n, k) (Python 3.8+), which are faster and avoid creating giant intermediate factorials.

Next: math.floor()

Return the floor of x, the largest integer <= x.

floor() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful