Зміст курсу
Python Functions: From Zero to Hero
Python Functions: From Zero to Hero
Default Parameters
Let's return to our function, which calculates the cost of our grocery cart. That is, we have the following function:
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)
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
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)
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:
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)
Завдання
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.
Дякуємо за ваш відгук!
Default Parameters
Let's return to our function, which calculates the cost of our grocery cart. That is, we have the following function:
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)
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
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)
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:
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)
Завдання
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.
Дякуємо за ваш відгук!
Default Parameters
Let's return to our function, which calculates the cost of our grocery cart. That is, we have the following function:
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)
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
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)
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:
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)
Завдання
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.
Дякуємо за ваш відгук!
Let's return to our function, which calculates the cost of our grocery cart. That is, we have the following function:
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)
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
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)
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:
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)
Завдання
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.