Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Compute Factorial Using Recursion

Python Compute Factorial Using Recursion

Document Object Reference Manual Python3 Examples

A factorial is a mathematical concept: the product of multiplying all integers from 1 up to a positive integer n. For example, the factorial of 5 (written 5!) is 1 * 2 * 3 * 4 * 5 = 120. We can compute a factorial using recursion, a technique in which a function calls itself.

Example

def

factorial

(

n

)

:

   

if

n

==

1

:

       

return

1

   

else

:

       

return

n * factorial

(

n -

1

)

Compute the factorial of 5

result

=

factorial

(

5

)

print

(

result

)

Code analysis:

Output result:

120

Document Object Reference Manual Python3 Examples