Calculating Fibonacci Sequence with a Function - Python Programming Exercise

In this exercise, you will develop a Python program that uses a recursive function to determine a number in the Fibonacci sequence. In this series, the first two numbers are 1, and each subsequent number is the sum of the previous two. This exercise is perfect for practicing function definition, recursion, and arithmetic operations in Python. By implementing this function, you will gain hands-on experience in handling function definitions, recursion, and arithmetic operations in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Calculating Fibonacci Sequence With A Function

 Objective

Develop a Python program that uses a recursive function to determine a number in the Fibonacci sequence. In this series, the first two numbers are 1, and each subsequent number is the sum of the previous two.

For example, you could use it like this: print(fibonacci(5))

 Example Python Exercise

 Copy Python Code
# Define the recursive function to calculate the Fibonacci number
def fibonacci(n):
    # Base case: if n is 1 or 2, return 1 (the first two numbers in the Fibonacci sequence)
    if n == 1 or n == 2:
        return 1
    # Recursive case: return the sum of the previous two Fibonacci numbers
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

# Main function to test the fibonacci function
def main():
    # Test the fibonacci function with n = 5
    print(fibonacci(5))  # Expected output: 5

# Call the main function to execute the program
main()

 Output

5

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Function to Alter a Character in a String

    In this exercise, you will develop a Python program with a function called "change_char" to alter a character at a specific position (0-based index) in a string, repl...

  •  Function to Determine Prime Status

    In this exercise, you will develop a Python program with a function called "is_prime" that takes an integer as input and returns True if the number is prime, or False...

  •  Main Function Parameters and Summation

    In this exercise, you will develop a Python program named "sum" that takes two integer numbers from the command line and prints their sum. This exercise is per...

  •  Function to Calculate Sum of Digits

    In this exercise, you will develop a Python program with a function named "sum_digits" that takes a number as input and returns the sum of its digits. This exercis...

  •  Function to Compute Factorial

    In this exercise, you will develop a Python program with a recursive function to compute the factorial of a given number. The factorial of a number is defined as: 𝑛...

  •  Main Function Parameters and Reversal

    In this exercise, you will develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. This exercise...