Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Function Parameters and Arguments

check

Creating functions that use input values.


Function Parameters and Arguments

Practice: Function to Greet in Different Languages

  1. Define a function called greet that takes two parameters: name and language.

  2. Inside the function, use an if statement to print different greetings based on the language. If the language is "Spanish", print "Hola, " + name + "!". If the language is "French", print "Bonjour, " + name + "!". If the language is not recognized, default to English and print "Hello, " + name + "!".

  3. After the function definition, call your greet function with the string "Alice" as the name and "Spanish" as the language.

  4. Run your code and confirm that the output is "Hola, Alice!".

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.
> "Hola, Alice!"
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(name, language):
    if language == "Spanish":
        print("Hola, " + name + "!")
    elif language == "French":
        print("Bonjour, " + name + "!")
    else:
        print("Hello, " + name + "!")
greet("Alice", "Spanish")

Practice: Function to Calculate the Area of a Rectangle

  1. Define a function called rectangle_area that takes two parameters: length and width.

  2. Inside the function, calculate the area of the rectangle (length multiplied by width) and print the result.

  3. After the function definition, call your rectangle_area function with the numbers 5 and 7 as the length and width, respectively.

  4. Run your code and confirm that the output is 35.

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.
> 35
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 rectangle_area(length, width):
    print(length * width)
rectangle_area(5, 7)

Practice: Function to Convert Celsius to Fahrenheit

  1. Define a function called celsius_to_fahrenheit that takes one parameter: celsius.

  2. Inside the function, convert the Celsius temperature to Fahrenheit (multiply by 9, divide by 5, then add 32) and print the result.

  3. After the function definition, call your celsius_to_fahrenheit function with the number 0 as the Celsius temperature.

  4. Run your code and confirm that the output is 32.

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.
> 32
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 celsius_to_fahrenheit(celsius):
    print(celsius * 9/5 + 32)
celsius_to_fahrenheit(0)