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 ▸

Use recursion to print all the sublists of a given list


Create a program that takes in two numbers and returns the highest of the two


What is the difference between a for and a while loop?


Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.