Зміст курсу
Python Functions: From Zero to Hero
Python Functions: From Zero to Hero
What is a Function?
Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.
A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
Now, let's turn this code into a function.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called.
To call a function, you have to use the name of the function followed by parentheses
. Let's look at an example.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.
Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.
Let's look at the syntax of a function in Python.
- In Python, the function is defined by
def
. - Then the name appears (the name is any legal name of a variable).
- The name of the function is followed by parentheses.
- Then there is a colon.
- Then we have a code indent that is executed inside the function.
- To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.
It's time to practice.
Завдання
You have to implement a function named function
, that displays data like this:
London is the capital and largest city of England and the United Kingdom
.
Дякуємо за ваш відгук!
What is a Function?
Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.
A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
Now, let's turn this code into a function.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called.
To call a function, you have to use the name of the function followed by parentheses
. Let's look at an example.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.
Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.
Let's look at the syntax of a function in Python.
- In Python, the function is defined by
def
. - Then the name appears (the name is any legal name of a variable).
- The name of the function is followed by parentheses.
- Then there is a colon.
- Then we have a code indent that is executed inside the function.
- To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.
It's time to practice.
Завдання
You have to implement a function named function
, that displays data like this:
London is the capital and largest city of England and the United Kingdom
.
Дякуємо за ваш відгук!
What is a Function?
Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.
A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
Now, let's turn this code into a function.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called.
To call a function, you have to use the name of the function followed by parentheses
. Let's look at an example.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.
Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.
Let's look at the syntax of a function in Python.
- In Python, the function is defined by
def
. - Then the name appears (the name is any legal name of a variable).
- The name of the function is followed by parentheses.
- Then there is a colon.
- Then we have a code indent that is executed inside the function.
- To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.
It's time to practice.
Завдання
You have to implement a function named function
, that displays data like this:
London is the capital and largest city of England and the United Kingdom
.
Дякуємо за ваш відгук!
Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.
A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
Now, let's turn this code into a function.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called.
To call a function, you have to use the name of the function followed by parentheses
. Let's look at an example.
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.
Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.
Let's look at the syntax of a function in Python.
- In Python, the function is defined by
def
. - Then the name appears (the name is any legal name of a variable).
- The name of the function is followed by parentheses.
- Then there is a colon.
- Then we have a code indent that is executed inside the function.
- To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.
It's time to practice.
Завдання
You have to implement a function named function
, that displays data like this:
London is the capital and largest city of England and the United Kingdom
.