Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Default Parameters | Assigning a Default Value to a Parameter
Python Functions: From Zero to Hero

Pyyhkäise näyttääksesi valikon

book
Default Parameters

Let's return to our function, which calculates the cost of our grocery cart. That is, we have the following function:

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(10, 7, 12)
copy

Suppose that in most stores, the price of oranges is the same (for example, 10 conventional units). Python allows you to set this price for oranges as the default value, which will be the same for all cases.

Let's look at the implementation of this

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges=10): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples = 11, price_of_carrots = 7)
copy

Now take a close look at the function call, in the code that is shown above. As you can see, we can now skip the third argument in the function call and just write the values of the first two arguments.

And oddly enough, our function will work correctly, since all the necessary data for calculating the total cost of a grocery basket is there.

Parameters without default values must be before parameters with default values.

In our example function, price_of_oranges = 10 should come after price_of_apples and after price_of_carrots. If we first write a parameter with a default value and then a parameter without a default value we will get: SyntaxError: non-default argument follows default argument. As in the example below:

1234
def grocery_cart(price_of_oranges=10, price_of_apples, price_of_carrots): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples = 11, price_of_carrots = 7)
copy
Tehtävä

Swipe to start coding

You have to implement a function named function(), which has three parameters: name, platform and age with a default value of age = 16. This function should display the following text: hello, my name is Max. I am 16 years old. I love Codefinity. Please, use f-strings to display the result.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

book
Default Parameters

Let's return to our function, which calculates the cost of our grocery cart. That is, we have the following function:

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(10, 7, 12)
copy

Suppose that in most stores, the price of oranges is the same (for example, 10 conventional units). Python allows you to set this price for oranges as the default value, which will be the same for all cases.

Let's look at the implementation of this

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges=10): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples = 11, price_of_carrots = 7)
copy

Now take a close look at the function call, in the code that is shown above. As you can see, we can now skip the third argument in the function call and just write the values of the first two arguments.

And oddly enough, our function will work correctly, since all the necessary data for calculating the total cost of a grocery basket is there.

Parameters without default values must be before parameters with default values.

In our example function, price_of_oranges = 10 should come after price_of_apples and after price_of_carrots. If we first write a parameter with a default value and then a parameter without a default value we will get: SyntaxError: non-default argument follows default argument. As in the example below:

1234
def grocery_cart(price_of_oranges=10, price_of_apples, price_of_carrots): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(price_of_apples = 11, price_of_carrots = 7)
copy
Tehtävä

Swipe to start coding

You have to implement a function named function(), which has three parameters: name, platform and age with a default value of age = 16. This function should display the following text: hello, my name is Max. I am 16 years old. I love Codefinity. Please, use f-strings to display the result.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt