# option 1 – iterate from 1 to n def factorial(n): # we start with the "n" itself result = n # for all numbers from 1 to n-1: for i in range(1,n): # multiply "result" by "i" result *= i return result print factorial(8) # option 2 – iterate from n to 1 def factorial(n): # we start with 1 result = 1 # for all numbers from 1 to n: for i in range(1,n+1): # multiply "result" by "i" result *= i return result print factorial(8) # option 3 – don't use loops at all! def factorial(n): # if n is equal to zero, return 1 (0! = 1) if n == 0: return 1 # if n in higher than zero, run the function again with n-1 → we will arrive to 0 in the end, which will return 1 (n * n-1 * n-2 * … * 2 * 1 * 1) return n * factorial(n - 1) print factorial(8)