Back to Practice
#0306

Factorial of a Number

EasyPython15 min10 XP

Problem

Given a non-negative integer n, return n!. By definition, 0! is 1.

Why This Matters

Factorial is a classic warm-up for loops, multiplication accumulation, base cases, and recursion thinking.

Function Signature

def factorial(n):

Examples

Example 1
Inputn = 5
Output120

5! means 5 * 4 * 3 * 2 * 1, which is 120.

Constraints

  • Return the exact requested value.
  • Handle edge cases cleanly.
  • Prefer clear Python code before clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Five factorial
Input5
Expected120

Multiply values from 2 through 5.

Zero factorial
Input0
Expected1

0! is defined as 1.

Hidden Test Categories
n = 1larger nnon-negative input only