Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 43

Factorial Program in Python

In this lesson: Find factorial in Python using loop, recursion and math.factorial — code with output and explanation. In Hindi & English.

What is a factorial

The factorial of a number n (written n!) is the product of all whole numbers from 1 up to n. For example, 5! = 1 × 2 × 3 × 4 × 5 = 120. We can compute it in Python with a loop, with recursion, or with a built-in function.

💡 Special rule: 0! = 1. Your code should return 1 when n is 0.

Factorial with a loop

n = 5
f = 1
for i in range(1, n + 1):
    f = f * i
print("Factorial:", f)
Factorial: 120

We start f at 1 and multiply it by each number from 1 to n. range(1, n+1) goes 1, 2, 3, 4, 5 — note the +1 so n is included.

Dry run of the loop

Dry Run — factorial loop (n=5): f is multiplied each time
i (range 1 to 5)f * if now
11 * 11
21 * 22
32 * 36
46 * 424
524 * 5120
Loop ends. Factorial = 120
💡 Understand it this way

f starts at 1 and gets multiplied by each number in turn (1×1×2×3×4×5). range(1, n+1) runs from 1 to n. 5! = 120.

Factorial with recursion

Recursion means a function that calls itself with a smaller value.

def factorial(n):
    if n == 0:          # base case
        return 1
    return n * factorial(n - 1)

print(factorial(5))
120

factorial(5) becomes 5 × factorial(4), then 4 × factorial(3), down to factorial(0) which returns 1. Then the results multiply back up to 120.

Using math.factorial

For real code, Python already has a factorial function.

import math
print(math.factorial(5))
120

math.factorial is the fastest and safest choice. You write your own loop or recursion mainly to understand how it works.

Common mistakes

  • Wrong range. Use range(1, n+1); without +1 you miss n itself.
  • Starting f at 0. Start at 1 — multiplying by 0 gives 0 forever.
  • Missing the base case in recursion. Without if n == 0: return 1, recursion never stops.
  • Recursion on huge numbers. Very large n can hit Python's recursion limit — use a loop or math.factorial.

Summary

  • Factorial n! is the product of all whole numbers from 1 to n.
  • With a loop: start f at 1 and multiply by each i in range(1, n+1).
  • With recursion: n * factorial(n-1), base case 0! = 1.
  • For real code, use math.factorial(n).
  • Remember 0! = 1 and that factorials grow very fast.

Next, try the prime number program, or revise Python loops.

Frequently Asked Questions

What is a factorial?
The factorial of a number n (written n!) is the product of all whole numbers from 1 up to n. For example 5! = 1×2×3×4×5 = 120. Factorials appear in maths, probability and combinations.
What is the factorial of 0?
By definition, 0! is 1. This is a special rule that keeps maths formulas consistent. Your loop or recursion should return 1 when n is 0.
Loop or recursion — which is better for factorial?
A loop is simple and efficient with no risk of stack overflow, so it is a good default. Recursion is elegant and mirrors the maths, but very large numbers can hit Python's recursion limit. For learning, try both.
Is there a built-in factorial in Python?
Yes. math.factorial(n) returns the factorial directly. It is the fastest and safest choice for real code — you only write your own loop or recursion to learn how it works.
Why does factorial grow so fast?
Each step multiplies by the next number, so the result explodes quickly — 10! is already over 3 million. Python handles big integers automatically, so it will not overflow like some other languages.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicPalindrome Program Your progress: 43 / 66 (65%) · Lesson 44 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free