Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Writing your own functions (1/5) | Functions
Learn Python from Scratch
course content

Зміст курсу

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

Writing your own functions (1/5)

In the previous examples (for example in the Conditional statements Section) we assigned different values to a variable just to check if our code works as we need. It might be uncomfortable, especially when your code is becoming larger and larger. This can be solved by using functions. To define function use syntax:

123
def name_of_function(var1, var2...): body of function return smth
copy

For example, let's define a simple function, which receives two numbers and returns their sum squared.

123456
# define function def sum_squared(a, b): return (a+b)**2 # call function print(sum_squared(2,3))
copy

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

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

Все було зрозуміло?

Секція 7. Розділ 2
toggle bottom row

Writing your own functions (1/5)

In the previous examples (for example in the Conditional statements Section) we assigned different values to a variable just to check if our code works as we need. It might be uncomfortable, especially when your code is becoming larger and larger. This can be solved by using functions. To define function use syntax:

123
def name_of_function(var1, var2...): body of function return smth
copy

For example, let's define a simple function, which receives two numbers and returns their sum squared.

123456
# define function def sum_squared(a, b): return (a+b)**2 # call function print(sum_squared(2,3))
copy

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

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

Все було зрозуміло?

Секція 7. Розділ 2
toggle bottom row

Writing your own functions (1/5)

In the previous examples (for example in the Conditional statements Section) we assigned different values to a variable just to check if our code works as we need. It might be uncomfortable, especially when your code is becoming larger and larger. This can be solved by using functions. To define function use syntax:

123
def name_of_function(var1, var2...): body of function return smth
copy

For example, let's define a simple function, which receives two numbers and returns their sum squared.

123456
# define function def sum_squared(a, b): return (a+b)**2 # call function print(sum_squared(2,3))
copy

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

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

Все було зрозуміло?

In the previous examples (for example in the Conditional statements Section) we assigned different values to a variable just to check if our code works as we need. It might be uncomfortable, especially when your code is becoming larger and larger. This can be solved by using functions. To define function use syntax:

123
def name_of_function(var1, var2...): body of function return smth
copy

For example, let's define a simple function, which receives two numbers and returns their sum squared.

123456
# define function def sum_squared(a, b): return (a+b)**2 # call function print(sum_squared(2,3))
copy

Завдання

Define a function my_first_function with three arguments which returns the sum of tripled first number, doubled second and third and all raised to the second power. For example, for 5, 4, 3 it should return (5⋅3 + 4⋅2 + 3)^2 = 676. Test it on that numbers.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 7. Розділ 2
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt