Exercise 1: Introduction to Recursion
Welcome to Python 2, Lesson 7! Today, we'll learn about recursion, a powerful programming concept where a function calls itself to solve a problem.
What is Recursion?
Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. In programming, recursion occurs when a function calls itself.
Let's start with a simple example of a recursive function that counts down from a given number:
Now it's your turn! Create a recursive function called count_up that counts up from 1 to a given number:
- Define the function
count_up(n) - If n is greater than 5, print "Done!"
- Otherwise, print the current number and call
count_up(n + 1) - Test your function with
count_up(1)
# Define your count_up function here
# Test your function
count_up(1)