About 698 letters

About 3 minutes

#Python's recursive functions

A function can call itself, which is called recursion. A typical example is calculating the Fibonacci sequence:

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)}")

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Note that you cannot have infinite self-calling. Python’s default recursion depth limit is 1000, and programs exceeding this limit will not run.

Updated: 5/21/2025

Author: PlanC

Created: 5/15/2025