Contenido del Curso
Python Functions Tutorial
1. What is Function in Python?
2. Positional and Optional Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions
Python Functions Tutorial
Return Value
Let's create a function walk_the_dog
that takes the dog's name dog_name
and the time for a walk walk_time
as parameters. The function should return
a message indicating whether it's time to walk the dog.
- The
if 6 <= walk_time <= 18:
condition checks if the walk time (walk_time
) is between 6 and 18 hours (inclusive); - If this condition is true (meaning the walk time is between 6 and 18 hours), the first code block is executed:This
return
statement returns a string indicating that it's time for a walk for the dog nameddog_name
; - If the condition is not true (meaning the walk time is not between 6 and 18 hours), the second code block is executed:In this case, the
return
statement returns a different message indicating that it's necessary to wait until 6 PM for the dog nameddog_name
to go for a walk.
So, the return
in this case specifies what result is returned from the function based on the condition. The function returns a string, which is then printed using print(message1)
and print(message2)
.
Absolutely, it's crucial to emphasize that the return
statement not only specifies the result to be returned but also immediately terminates the execution of the function. Once a return
statement is encountered, the function exits, and any subsequent code within the function is not executed.
Tarea
Create a function greet_dog
that takes the dog's name and breed as arguments. The function should return
a string that greets the dog and provides information about its breed.
Your task is to complete the greet_dog
function so that it returns the appropriate greeting.
¿Todo estuvo claro?
Contenido del Curso
Python Functions Tutorial
1. What is Function in Python?
2. Positional and Optional Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions
Python Functions Tutorial
Return Value
Let's create a function walk_the_dog
that takes the dog's name dog_name
and the time for a walk walk_time
as parameters. The function should return
a message indicating whether it's time to walk the dog.
- The
if 6 <= walk_time <= 18:
condition checks if the walk time (walk_time
) is between 6 and 18 hours (inclusive); - If this condition is true (meaning the walk time is between 6 and 18 hours), the first code block is executed:This
return
statement returns a string indicating that it's time for a walk for the dog nameddog_name
; - If the condition is not true (meaning the walk time is not between 6 and 18 hours), the second code block is executed:In this case, the
return
statement returns a different message indicating that it's necessary to wait until 6 PM for the dog nameddog_name
to go for a walk.
So, the return
in this case specifies what result is returned from the function based on the condition. The function returns a string, which is then printed using print(message1)
and print(message2)
.
Absolutely, it's crucial to emphasize that the return
statement not only specifies the result to be returned but also immediately terminates the execution of the function. Once a return
statement is encountered, the function exits, and any subsequent code within the function is not executed.
Tarea
Create a function greet_dog
that takes the dog's name and breed as arguments. The function should return
a string that greets the dog and provides information about its breed.
Your task is to complete the greet_dog
function so that it returns the appropriate greeting.
¿Todo estuvo claro?