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

1667 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Write a recursive function to return the nth Fibonacci number in Python


Define a function that takes in the age of the user and adds it to an output sentence such as "You are ..... old".


Make a program that asks the user for a series of numbers until they either want to output the average or quit the program.


how to compute the factorial of a number.


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