Course Content
Python Functions Tutorial
1. What is Function in Python?
3. Function Return Value Specification
4. Some Additional Topics
Python Functions Tutorial
Arguments
In Python, arguments (also called inputs or parameters) of the function are the inputs that are passed to a function when it is called. They allow you to provide data or values to the function, which the function can then use to perform its operations. Arguments of the function can be both single values and objects (list, tuple, dictionary, custom objects, etc.).
We have already used print()
function - we have to specify string as an argument of this function. Let's look at another example:
Assume that we want to write the function that calculates the sum of two different numbers and prints the result. To do it we have to pass these two numbers to the function. We have to do it using ()
brackets.
Note
You may noticed that we use letter
f
at the beginning of the string as the argument ofprint()
function. Such construction is called f-string. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.
We have passed num_1
and num_2
as an arguments of the function and then used them to calculate the sum. We can set an arbitrary number of function arguments. Next, you will solve a problem with 4 function arguments.
Now let's consider an example when we use a list as an argument of the function:
Task
Assume that you have to calculate perimeter of the triangle with sides a
, b
and c
. Write the function with calculates perimeter and prints it in console.
Everything was clear?