How to Calculate the Factorial of a Number in Python

def factorial(n):
    return 1 if n == 0 else n * factorial(n - 1)

number = 5
result = factorial(number)
print(f"The factorial of {number} is {result}")

Output:

The factorial of 5 is 120

Leave a Comment

Your email address will not be published.