Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Functions in Python

check

Creating your own custom functions in Python.


Practice: Greeting User Function

  1. Define a function named greet_user.

  2. Inside the function, use the print function to output the string "Hello, user!".

  3. After the function definition, call your greet_user function.

  4. Run your code and confirm that the output is "Hello, user!".

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, user!"
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 greet_user():
    print("Hello, user!")
greet_user()

Practice: Function to Print a Favorite Quote

  1. Define a function named favorite_quote.

  2. Inside the function, use the print function to output your favorite quote.

  3. After the function definition, call your favorite_quote function.

  4. Run your code and confirm that the output is your favorite quote.

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.
> "The only way to do great work is to love what you do." - Steve Jobs
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 favorite_quote():
    print('"The only way to do great work is to love what you do." - Steve Jobs')
favorite_quote()

Practice: Function to Print a List of Fruits

  1. Define a function named print_fruits.

  2. Inside the function, define a list with the names of some fruits.

  3. Also in the function, use a for loop to print each fruit in the list.

  4. After the function definition, call your print_fruits function.

  5. Run your code and confirm that the output is a list of fruits, each printed on a new line.

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.
> "apple"
> "banana"
> "cherry"
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 print_fruits():
    fruits = ["apple", "banana", "cherry"]
    for fruit in fruits:
        print(fruit)

print_fruits()

Practice: Function to Print a Countdown

  1. Define a function named countdown.

  2. Inside the function, use a for loop and the range function to count down from 5 to 1 (inclusive), printing each number on a new line.

  3. After the last number, print the string "Blast off!".

  4. After the function definition, call your countdown function.

  5. Run your code and confirm that the output is a countdown from 5 to 1, followed by "Blast off!".

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.
> 

5 4 3 2 1 Blast off!

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 countdown():
    for i in range(5, 0, -1):
        print(i)
    print("Blast off!")
countdown()