Зміст курсу
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 distance between New York and Boston and display the result.
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
Now, let's turn this code into a function.
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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 calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no call to the function, 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 distance between New York and Boston and display the result.
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
Now, let's turn this code into a function.
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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 calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no call to the function, 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 distance between New York and Boston and display the result.
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
Now, let's turn this code into a function.
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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 calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no call to the function, 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 distance between New York and Boston and display the result.
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
Now, let's turn this code into a function.
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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 calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no call to the function, 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
.