Implement a fibonacci function which calculates the nth number of the fibonacci sequence.

def fib(n):  if n <= 1:    return n  else:    return(fib(n-1) + fib(n-2))
This is the code for a very basic version. This coud be used to explain how recursion works thanks to a relatively easy example.

AF

Related Python Mentoring answers

All answers ▸

What can be used to iterate through a list in python?


What are recursive functions and when should they be used?


How do I use format python code?


Ask the user for a number and output the smallest divisor, bigger than one, for the inputted number. Output "Prime" if the number is a prime number.