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.

Answered by Atom F. Python tutor

818 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Sort a given input of lowercase letters alphabetically


h


How do you write code to implement a recursive fibonacci algorithm?


Write a function s(n) that will return a list of the first n squares.


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy