python example

Factorial in Python

Compute n! with a loop. A short Python snippet for the online compiler.

Start at 1 and multiply up to n. factorial(0) should stay 1 because the loop never runs.

Keep the function tiny so you can read the traceback if you break it on purpose.

def factorial(n):
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

print(factorial(5))
print(factorial(0))