Content · Programming · Python

Python math.factorial() Function: Usage and Examples

Compute the product of positive integers up to a nonnegative integer.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Compute the product of positive integers up to a nonnegative integer.

What You Will Learn


In Python this math.factorial name is a soft-deprecated compatibility alias for math.integer.factorial. Factorial(0) is 1; negative or nonintegral inputs are invalid.

For floating-point results, remember that stored values are approximations. Rounding a displayed value does not change the exact value used by later calculations.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

import math
print(math.factorial(6))

How the Example Works


  • The imports provide the standard-library tools used in the demonstration: math.
  • The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.

For the chosen input, the expression math.factorial(6) prints 720. Factorial multiplies all positive integers up to its argument, with 0! defined as 1.

Expected output from the example:

720

Another Example: Compare the Results


Try a second input and compare it with the first. Notice whether the result changes in type, value, order, or number of items.

import math
print(math.factorial(0), math.factorial(5))

Expected output:

1 120

When You Would Use This


This operation can support measurements, numeric reports, simulations, or input validation. Choose integer operations when exact integer answers are required, and check floating-point rounding when displaying measured values. Validate units and input domains before using the result in a calculation.

Practice: Modify the Code


Count all orderings of five unique items.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Passing a float such as 6.0 and assuming it is accepted as an integer.
  • Skipping invalid or boundary inputs when adapting a numeric function to real user data.

Key Takeaways


  • Compute the product of positive integers up to a nonnegative integer.
  • In Python this math.factorial name is a soft-deprecated compatibility alias for math.integer.factorial.
  • Apply this operation only after checking the assumptions and input conditions shown in the example.
Share this resourceSend this page to someone who may find it useful.