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 ▸

Which four data types are used in Python? Can you give an example of each?


[Student query] Why do we import libraries/modules such as Numpy and Matplotlib into Python?


Give a segment of python code that will print the numbers 1 to 7, each one on a new line


What are recursive functions and when should they be used?