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

1662 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What is the difference between using range() and a list of values?


Whats the output of "for i in "hello world": print(i)" and why/how is this achieved


What is the difference between a FOR loop and WHILE loop and how do I write them?


How does a for loop work in Python?


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