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

1542 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

What is the difference between a cycle "if" and "while"?


Write a simple Python program that asks a user to input their age and name and then return the text: "Hi my name is *NAME*, and I am *AGE* years old."


Write a program that computes the sum of all numbers up to a input number n


Write a recursive function that takes any integer n and prints the nth Fibonacci 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