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
Answered by Atom F. Python tutor

1290 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What is the outcome of the following code? print(2*[3,4,5])


Give an example of a FOR loop


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


Using the shared code editor, write a recursive function for calculating a factorial of an input parameter.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences