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

1312 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

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


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


What is the difference between a list and an array?


Give a segment of python code that will print the numbers 1 to 7, each one on a new line


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