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

1320 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

How could I write a program to convert Km/h to m/s in python


When to use a dectionary or a list


Write a function that takes a string, and outputs that string formatted in camelcase. (alternating upper and lower case for each character, e.g. cAmElCaSe)


Give an example of a FOR loop


We're here to help

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

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences