Contenido del Curso
Python Functions Tutorial
Python Functions Tutorial
Recursion
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).
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
Let's go step by step through how this recursive program works:
- Base Case Check: The function checks if
times
is greater than 0. In our case,times
is 3, satisfying the condition; - Print Message: The function prints the message
"Hello, Recursion!"
; - Recursive Call: The function calls itself with the same message and
times - 1
(which is 2 in this case); - 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); - 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; - Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
- 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:
- Base case: if the number is less than
10
the function must return this number as the result. - 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 (//
).
¡Gracias por tus comentarios!
Recursion
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).
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
Let's go step by step through how this recursive program works:
- Base Case Check: The function checks if
times
is greater than 0. In our case,times
is 3, satisfying the condition; - Print Message: The function prints the message
"Hello, Recursion!"
; - Recursive Call: The function calls itself with the same message and
times - 1
(which is 2 in this case); - 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); - 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; - Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
- 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:
- Base case: if the number is less than
10
the function must return this number as the result. - 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 (//
).
¡Gracias por tus comentarios!
Recursion
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).
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
Let's go step by step through how this recursive program works:
- Base Case Check: The function checks if
times
is greater than 0. In our case,times
is 3, satisfying the condition; - Print Message: The function prints the message
"Hello, Recursion!"
; - Recursive Call: The function calls itself with the same message and
times - 1
(which is 2 in this case); - 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); - 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; - Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
- 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:
- Base case: if the number is less than
10
the function must return this number as the result. - 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 (//
).
¡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).
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # Function call print_message("Hello, Recursion!", 3)
Let's go step by step through how this recursive program works:
- Base Case Check: The function checks if
times
is greater than 0. In our case,times
is 3, satisfying the condition; - Print Message: The function prints the message
"Hello, Recursion!"
; - Recursive Call: The function calls itself with the same message and
times - 1
(which is 2 in this case); - 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); - 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; - Completion of Calls: Each recursive call completes, and control is passed back to the previous call;
- 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:
- Base case: if the number is less than
10
the function must return this number as the result. - 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 (//
).