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

Python Exercise Example 26

Python Exercise Example 26

Python 100 Examples Python 100 Examples

**Question:** Use recursion to find 5!.

**Program analysis:** Recursive formula: fn=fn_1*4!

Program source code:

Example

#!/usr/bin/python

-- coding: UTF-8 --

def

fact

(

j

)

:

sum

=

0

if

j

==

0

:

sum

=

1

else

:

sum

=

j

fact

(

j

1

)

return

sum

print

(

fact

(

5

)

)

The output of the above example is:


120

Python 100 Examples Python 100 Examples