Generate Fibonacci Sequence in Python

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. It starts with 0 and 1, and the next number is the sum of the previous two numbers. In this challenge, you will write a function that generates a list containing the Fibonacci sequence up until the nth term.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13630{{"`Generate Fibonacci Sequence in Python`"}} python/conditional_statements -.-> lab-13630{{"`Generate Fibonacci Sequence in Python`"}} python/while_loops -.-> lab-13630{{"`Generate Fibonacci Sequence in Python`"}} python/lists -.-> lab-13630{{"`Generate Fibonacci Sequence in Python`"}} python/function_definition -.-> lab-13630{{"`Generate Fibonacci Sequence in Python`"}} python/build_in_functions -.-> lab-13630{{"`Generate Fibonacci Sequence in Python`"}} end

Fibonacci

Write a function called fibonacci(n) that takes an integer n as its parameter and returns a list containing the Fibonacci sequence up until the nth term.

To solve this problem, you can follow these steps:

  1. Create an empty list called sequence.
  2. If n is less than or equal to 0, append 0 to the sequence list and return the list.
  3. Append 0 and 1 to the sequence list.
  4. Use a while loop to add the sum of the last two numbers of the sequence list to the end of the list, until the length of the list reaches n.
  5. Return the sequence list.
def fibonacci(n):
  if n <= 0:
    return [0]
  sequence = [0, 1]
  while len(sequence) <= n:
    next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2]
    sequence.append(next_value)
  return sequence
fibonacci(7) ## [0, 1, 1, 2, 3, 5, 8, 13]

Summary

In this challenge, you have learned how to generate a list containing the Fibonacci sequence up until the nth term. You have also learned how to use a while loop to add the sum of the last two numbers of a list to the end of the list.

Other Python Tutorials you may like