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

1376 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

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


How does a for loop work in Python?


Write a program that takes a value x and then outputs x Fibonnaci numbers. E.g. if x=6 output would be 1 1 2 3 5 8


[Student query] Why do we import libraries/modules such as Numpy and Matplotlib into 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:

© 2025 by IXL Learning