Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Recursion | Recursion and Lambda Functions
Python Functions Tutorial
course content

Contenido del Curso

Python Functions Tutorial

Python Functions Tutorial

1. What is Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

bookRecursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

1234567
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
copy

Let's go step by step through how this recursive program works:

  1. Base Case Check: The function checks if times is greater than 0. In our case, times is 3, satisfying the condition;
  2. Print Message: The function prints the message "Hello, Recursion!";
  3. Recursive Call: The function calls itself with the same message and times - 1 (which is 2 in this case);
  4. New Call: In the new call, the same process repeats: base case check, printing the message, and another recursive call with times - 1 (now 1);
  5. Final Call: The final call happens for times = 1, the last message is printed, and immediately the recursion exits as the base case condition is met;
  6. Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
  7. Program Termination: The program returns to the initial call, and the program execution completes.

The result will be the printing of the message "Hello, Recursion!" three times.

Tarea

Your task is to fill in the blanks (___) in the function that will calculate the sum of the digits of a positive number using recursion:

  1. Base case: if the number is less than 10 the function must return this number as the result.
  2. Recursive case: firstly, we extract the last digit from the number using the modulus operator (%) and then add this number to the sum of the remaining digits. The remaining digits can be found using the integer division operator (//).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1
toggle bottom row

bookRecursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

1234567
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
copy

Let's go step by step through how this recursive program works:

  1. Base Case Check: The function checks if times is greater than 0. In our case, times is 3, satisfying the condition;
  2. Print Message: The function prints the message "Hello, Recursion!";
  3. Recursive Call: The function calls itself with the same message and times - 1 (which is 2 in this case);
  4. New Call: In the new call, the same process repeats: base case check, printing the message, and another recursive call with times - 1 (now 1);
  5. Final Call: The final call happens for times = 1, the last message is printed, and immediately the recursion exits as the base case condition is met;
  6. Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
  7. Program Termination: The program returns to the initial call, and the program execution completes.

The result will be the printing of the message "Hello, Recursion!" three times.

Tarea

Your task is to fill in the blanks (___) in the function that will calculate the sum of the digits of a positive number using recursion:

  1. Base case: if the number is less than 10 the function must return this number as the result.
  2. Recursive case: firstly, we extract the last digit from the number using the modulus operator (%) and then add this number to the sum of the remaining digits. The remaining digits can be found using the integer division operator (//).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 1
toggle bottom row

bookRecursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

1234567
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
copy

Let's go step by step through how this recursive program works:

  1. Base Case Check: The function checks if times is greater than 0. In our case, times is 3, satisfying the condition;
  2. Print Message: The function prints the message "Hello, Recursion!";
  3. Recursive Call: The function calls itself with the same message and times - 1 (which is 2 in this case);
  4. New Call: In the new call, the same process repeats: base case check, printing the message, and another recursive call with times - 1 (now 1);
  5. Final Call: The final call happens for times = 1, the last message is printed, and immediately the recursion exits as the base case condition is met;
  6. Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
  7. Program Termination: The program returns to the initial call, and the program execution completes.

The result will be the printing of the message "Hello, Recursion!" three times.

Tarea

Your task is to fill in the blanks (___) in the function that will calculate the sum of the digits of a positive number using recursion:

  1. Base case: if the number is less than 10 the function must return this number as the result.
  2. Recursive case: firstly, we extract the last digit from the number using the modulus operator (%) and then add this number to the sum of the remaining digits. The remaining digits can be found using the integer division operator (//).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

1234567
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
copy

Let's go step by step through how this recursive program works:

  1. Base Case Check: The function checks if times is greater than 0. In our case, times is 3, satisfying the condition;
  2. Print Message: The function prints the message "Hello, Recursion!";
  3. Recursive Call: The function calls itself with the same message and times - 1 (which is 2 in this case);
  4. New Call: In the new call, the same process repeats: base case check, printing the message, and another recursive call with times - 1 (now 1);
  5. Final Call: The final call happens for times = 1, the last message is printed, and immediately the recursion exits as the base case condition is met;
  6. Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
  7. Program Termination: The program returns to the initial call, and the program execution completes.

The result will be the printing of the message "Hello, Recursion!" three times.

Tarea

Your task is to fill in the blanks (___) in the function that will calculate the sum of the digits of a positive number using recursion:

  1. Base case: if the number is less than 10 the function must return this number as the result.
  2. Recursive case: firstly, we extract the last digit from the number using the modulus operator (%) and then add this number to the sum of the remaining digits. The remaining digits can be found using the integer division operator (//).

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 5. Capítulo 1
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt