Easy Tutorial
❮ Python String Isupper Python Os Renames ❯

Python Factorial Example

Python3 Examples

The factorial of an integer (English: factorial) is the product of all positive integers less than or equal to that number, with the factorial of 0 being 1. That is: n! = 1 × 2 × 3 × ... × n.

Example

#!/usr/bin/python3

# Filename : test.py
# author by : www.tutorialpro.org

# Calculate the factorial of a number entered by the user

# Get the number from the user
num = int(input("Please enter a number: "))
factorial = 1

# Check if the number is negative, zero, or positive
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1, num + 1):
       factorial = factorial * i
   print("The factorial of %d is %d" % (num, factorial))

Executing the above code gives the following result:

Please enter a number: 3
The factorial of 3 is 6

Python3 Examples

❮ Python String Isupper Python Os Renames ❯