Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] Finding the factorial of a number in Python
#1
To find the factorial of a number you need to know what a factorial is..I assume you do, but if you don't, here:

A factorial is a number multiplied by one less than itself until it reaches 0 (exclusive)...

So a factorial of 4 is : 4*3, 4*2, 4*1 = 24

Here's how to do that in Python using a ternary expression making it super short:

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

print factorial(4)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)