Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Default Parameter Values

In the previous exercise, you saw that a call with a missing argument ends with an error. But often there's a value that fits most of the time, and we only want to pass a different one now and then. Most people order a medium coffee, for example. For such cases, a parameter can have a default value: after the name of the parameter, write = and the value:

def order_coffee(drink, size="medium"):
    print(f"{drink}, size: {size}")

order_coffee("Latte", "large")
order_coffee("Espresso")

This prints:

Latte, size: large
Espresso, size: medium

When the call passes an argument for size, the function uses it. When it doesn't, the function uses the default value "medium". Without the default value, the second call would end with the error TypeError: order_coffee() missing 1 required positional argument: 'size'.

Notice that there are no spaces around the = of a default value: size="medium", unlike when you create a variable: size = "medium". It would work with spaces too; it's just how Python programmers write it.

Because arguments go into parameters in order, parameters with default values go at the end. Then you can leave out the last arguments, and the ones you pass still go into the right parameters. Python won't even let you put a parameter without a default value after one with a default value: def order_coffee(size="medium", drink): ends with the error SyntaxError: parameter without a default follows parameter with a default.

Instructions

  1. 1

    Our shop shows most prices in euros, so the last call passes only the price. Run the program: it prints two prices and then ends with an error. Give the parameter currency the default value "EUR". The last line then says Price: 25 EUR, and the other two calls still print their own currencies.

  2. 2

    Below the function print_price, create a function greet with a parameter name whose default value is "visitor". The function prints a greeting such as Hello, Anna!.

  3. 3

    At the end of the program, call greet once without an argument, and once with your name as the argument. The program prints Hello, visitor! and then the greeting with your name.

Back

4/10

  1. 1. Defining Functions
  2. 2. Calling Functions
  3. 3. Parameters and Arguments
  4. 4. Default Parameter Values
  5. 5. Returning Values
  6. 6. Print or Return?
  7. 7. Return Ends the Function
  8. 8. Local Variables
  9. 9. Functions and Lists
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.