Thinking Recursively In Python

Three examples to start the learning process

Photo by Kevin Bergen on Unsplash
def nonrec_factorial(n):
result = 1
for i in range(1,n+1):
result = result * i
return result
def factorial(n):
while n != 0:
if n == 1:
return 1
return n * factorial(n-1)
def sumHarmonics(n):
if n == 0:
return 0
else:
return ( 1/n + sumHarmonics (n — 1))
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
return (fib(n-1) + fib(n-2))
                                Fib(4)
/ \
Fib(3) + Fib(2)
/ \ / \
Fib(2) + Fib(1) + Fib(1) + Fib(0)
/ \
Fib(1) + Fib(0)