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

1616 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What can I do with Python?


What are docstrings and how do I use them to improve my code readability?


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.


How do I use format python code?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

MyTutor is part of the IXL family of brands:

© 2026 by IXL Learning