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

Зміст курсу

Python Functions: From Zero to Hero

Python Functions: From Zero to Hero

1. What is a Function?
2. Arguments or Parameters
3. Assigning a Default Value to a Parameter
4. An Unknown Number of Arguments
5. Functions within Functions
6. Recursion

bookDefault 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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
toggle bottom row

bookDefault 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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 1
toggle bottom row

bookDefault 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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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

Завдання

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.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt