Python BasicsEasy

Factorial of a number

Detailed guide and Python implementation for the 'Factorial of a number' problem.

Problem Statement

Easy

Write a function factorial(n) that takes a non-negative integer n and returns its factorial. The factorial of n (written as n!) is the product 1 × 2 × 3 × ... × n. By definition, 0! = 1.

Constraints
  • 0 <= n <= 20

Examples

Example 1
Input
n = 5
Output
120
Explanation

5! = 1 × 2 × 3 × 4 × 5 = 120.

Example 2
Input
n = 0
Output
1
Explanation

0! is defined as 1.

Example 3
Input
n = 10
Output
3628800
Explanation

10! = 3628800.

Need a Hint?
Consider using Basics-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.