#Python's recursive functions
A function can call itself, which is called recursion. A typical example is calculating the Fibonacci sequence:
The Fibonacci sequence is defined by the formula
, where and .
def fibonacci(n: int) -> int:
if n <= 0:
return 0
elif n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
n: int = int(input("Please enter the term number n: "))
print(f"The {n}th term of the Fibonacci sequence is {fibonacci(n)}")
Note that you cannot have infinite self-calling. Python’s default recursion depth limit is 1000, and programs exceeding this limit will not run.