Зміст курсу
Python Functions: From Zero to Hero
Python Functions: From Zero to Hero
Arguments or Parameters
Pay attention to the parentheses, when creating a function
def grocery_cart()
and when calling a function
grocery_cart()
.
They should not be empty.
The data you specify in parentheses is passed to the function so that the function can use it during its execution.
Let's see how it works! Suppose we call the grocery_cart(10, 7, 12)
function instead of grocery_cart()
. That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.
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)
We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.
Parameters or arguments?
A parameter is a variable that we specify in parentheses when we define the function.
An argument is a value that we specify in parentheses when calling the function.
Arguments in the function call - numbers 10
, 7
and 12
. Parameters in the definition of the function -- price_of_apples
, price_of_carrots
and price_of_oranges
. Accordingly, these variables contain the numbers that are arguments, ie: 10
, 7
and 12
, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples
is 12
price_of_carrots
is 8
price_of_oranges
is 9
. Therefore, in this case, we have the following piece of code:
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(12, 8, 9)
But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12
is the first argument, it passes to the first parameter, price_of_apples
. Since 8
is the second argument, it passes to the second parameter price_of_carrots
. Since 9
is the third argument, it passes to the third parameter price_of_oranges
.
The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.
Завдання
You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.
To test, take these values as arguments
height = 5, width = 23
Дякуємо за ваш відгук!
Arguments or Parameters
Pay attention to the parentheses, when creating a function
def grocery_cart()
and when calling a function
grocery_cart()
.
They should not be empty.
The data you specify in parentheses is passed to the function so that the function can use it during its execution.
Let's see how it works! Suppose we call the grocery_cart(10, 7, 12)
function instead of grocery_cart()
. That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.
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)
We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.
Parameters or arguments?
A parameter is a variable that we specify in parentheses when we define the function.
An argument is a value that we specify in parentheses when calling the function.
Arguments in the function call - numbers 10
, 7
and 12
. Parameters in the definition of the function -- price_of_apples
, price_of_carrots
and price_of_oranges
. Accordingly, these variables contain the numbers that are arguments, ie: 10
, 7
and 12
, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples
is 12
price_of_carrots
is 8
price_of_oranges
is 9
. Therefore, in this case, we have the following piece of code:
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(12, 8, 9)
But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12
is the first argument, it passes to the first parameter, price_of_apples
. Since 8
is the second argument, it passes to the second parameter price_of_carrots
. Since 9
is the third argument, it passes to the third parameter price_of_oranges
.
The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.
Завдання
You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.
To test, take these values as arguments
height = 5, width = 23
Дякуємо за ваш відгук!
Arguments or Parameters
Pay attention to the parentheses, when creating a function
def grocery_cart()
and when calling a function
grocery_cart()
.
They should not be empty.
The data you specify in parentheses is passed to the function so that the function can use it during its execution.
Let's see how it works! Suppose we call the grocery_cart(10, 7, 12)
function instead of grocery_cart()
. That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.
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)
We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.
Parameters or arguments?
A parameter is a variable that we specify in parentheses when we define the function.
An argument is a value that we specify in parentheses when calling the function.
Arguments in the function call - numbers 10
, 7
and 12
. Parameters in the definition of the function -- price_of_apples
, price_of_carrots
and price_of_oranges
. Accordingly, these variables contain the numbers that are arguments, ie: 10
, 7
and 12
, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples
is 12
price_of_carrots
is 8
price_of_oranges
is 9
. Therefore, in this case, we have the following piece of code:
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(12, 8, 9)
But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12
is the first argument, it passes to the first parameter, price_of_apples
. Since 8
is the second argument, it passes to the second parameter price_of_carrots
. Since 9
is the third argument, it passes to the third parameter price_of_oranges
.
The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.
Завдання
You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.
To test, take these values as arguments
height = 5, width = 23
Дякуємо за ваш відгук!
Pay attention to the parentheses, when creating a function
def grocery_cart()
and when calling a function
grocery_cart()
.
They should not be empty.
The data you specify in parentheses is passed to the function so that the function can use it during its execution.
Let's see how it works! Suppose we call the grocery_cart(10, 7, 12)
function instead of grocery_cart()
. That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.
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)
We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.
Parameters or arguments?
A parameter is a variable that we specify in parentheses when we define the function.
An argument is a value that we specify in parentheses when calling the function.
Arguments in the function call - numbers 10
, 7
and 12
. Parameters in the definition of the function -- price_of_apples
, price_of_carrots
and price_of_oranges
. Accordingly, these variables contain the numbers that are arguments, ie: 10
, 7
and 12
, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples
is 12
price_of_carrots
is 8
price_of_oranges
is 9
. Therefore, in this case, we have the following piece of code:
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(12, 8, 9)
But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12
is the first argument, it passes to the first parameter, price_of_apples
. Since 8
is the second argument, it passes to the second parameter price_of_carrots
. Since 9
is the third argument, it passes to the third parameter price_of_oranges
.
The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.
Завдання
You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.
To test, take these values as arguments
height = 5, width = 23