

How regular and anonymous functions work in Python
A function in Python is an object that takes arguments and returns a value. You have already encountered built-in functions in previous lessons. For example, when we wanted to know the sum of all the numbers in a list. You can take a look at the issue about lists .
Then we used the built-in sum() function. We passed an argument as input - a list of numbers, and the function returned us the result - the sum of these numbers.

If such a function didn't exist in Python, we could write something like this:

And this is just a few lines of code, albeit very simple. And with the function we can replace them with just one word, which is much more convenient. In addition, the function is designed in a more universal way. Whatever list we give her, she will still return the sum of its elements to us. It doesn’t matter what this list will be called or whether it will have a name at all. The main thing is that there are numbers on the list!
Subscribe to the Workshop newsletter You will learn about cool tools for collecting, analyzing and visualizing dataAnd in our code, where we ourselves calculate the amount, we are talking about a specific list called my_list. That is, without changing the name, this code cannot be used for another list. Therefore, if the sum function did not exist in Python, then it would be more convenient for us to write it ourselves. Let's turn our piece of code into a function.
To define a function, use the def construct. Next we write the name of the function, parentheses and a colon. After the colon, you write in code what exactly the function should do, and then using return the function returns the result. And to use a function, you need to write its name in parentheses and pass it an argument.

This is what the function will almost always look like.

But it doesn’t always have to accept something as input or return something. Here is an example of a function that simply says hello - prints "Hi!", but takes no arguments and returns nothing. And if you write print(say_hi()) , then you will see None in the response.

A function may not take anything as input, but still return something rather than just print it out. For example, here is a function that asks the user for a name and returns it.

Moreover, the function can accept and return almost any objects and data. Here is a function that takes a person's name as input and greets them. But we can pass it not just a name, not in the form of a string with it, but our get_name() function. It will ask the user for their name and pass it to a function that will greet them.

There is also a separate type of arguments for functions. One of them is an optional argument. For example, we want to improve our say_hi_to() function. Now she should not only greet the user, but also start a small conversation with him. Therefore, we will add an optional argument small_talk to the function.

By default we assigned small_talk = “How are you?” . When we call the function, we do not need to specify this argument, it is already there by default. We only need to pass the name, then the function itself will ask the user “How are you?”
Do you like our lectures? Support us Your donation will help us prepare even more Workshop episodesSupport “Important Stories”We need to separately specify the small_talk argument when exporting a function only when we want to make some changes to it. For example, we want that instead of “How are you?” the function asked something else.

Let's see how functions can be used in practice. Let's write a function that will clear a phone number from spaces, hyphens and parentheses. Let's say we have a dataset that contains data about the owners of phone numbers. And we would need to bring all phones to the same format. First we will write a function, and then use it for the dataset.

Let's say we store our data in the form of a similar dictionary - phone_book = {'Alice': '8-987-657 12 11', 'Bob': '89012345678', 'Jack': '8 (9 1 2) 3 4 5 6 7 8 9'}. The keys here are names, and the values are phone numbers, which are written in different ways: with spaces, hyphens, parentheses. We need to accept the clean_tel() function for each of these numbers.
Let's now write a function that will do exactly this. It will need to pass a dictionary with “dirty” numbers as input, and it will return a list with clean data.

Such a list can be easily saved in a table.
Python has anonymous functions called lambdas. They follow a stricter but more concise syntax than regular Python functions. And I work faster. Anonymous functions can only contain one expression. They are created using the lambda instruction. In addition, they do not have to be assigned to a variable.
To make it easier, let's first write an already known regular function, and then rewrite it using lambda. Here is an example of a function that simply adds two numbers.

And here's what it looks like if you rewrite it using lambda .

After the lambda instruction, you need to specify the arguments, followed by a colon what the function should do with these arguments.
The lambda function does not have to be named anything. She can be called without a name. You need to put the function in parentheses. And next to it, indicate the arguments in parentheses.

By the way, our say_hi() function can also be rewritten using lambda.

These are all the basic principles of how functions work in Python. If you'd like to read about more feature options, you can do so here . And, as always, the Jupyter Notebook from this lesson is on our GitHub .
Support those