Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Return Statement

check

Making Python functions give values back with return statements.


Function return Statement

Practice: Function to Multiply Numbers

  1. Define a function called multiply that takes two parameters: num1 and num2.

  2. Inside the function, multiply num1 and num2 and return the result.

  3. After the function definition, call your multiply function with the numbers 6 and 7. Assign the return value to a variable named product.

  4. Print product.

  5. Run your code and confirm that the output is 42.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> 42
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
def multiply(num1, num2):
    return num1 * num2
product = multiply(6, 7)
print(product)

Practice: Function to Capitalize a String

  1. Define a function called capitalize_word that takes one parameter: word.

  2. Inside the function, capitalize the first letter of word and return the result. The Pyton built in capitalize() function will help with this.

  3. After the function definition, call your capitalize_word function with the string "hello". Assign the return value to a variable named word_capitalize.

  4. Print capitalized_word.

  5. Run your code and confirm that the output is "Hello".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> "Hello"
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
def capitalize_word(word):
    return word.capitalize()
word_capitalized = capitalize_word("hello")
print(word_capitalized)

Practice: Function to Check if a Number is Odd or Even

  1. Define a function called check_odd_even that takes one parameter: num.

  2. Inside the function, use an if statement to check whether num is even. If it's even, return the string "Even". If it's not, return the string "Odd".

  3. After the function definition, call your check_odd_even function with the number 15. Assign the return value to a variable named result.

  4. Print result.

  5. Run your code and confirm that the output is "Odd".

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> "Odd"
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
def check_odd_even(num):
    if num % 2 == 0:
        return "Even"
    else:
        return "Odd"
result = check_odd_even(15)
print(result)