Functions in Python: a small guide for a real Python Ninja!

Soledad Musella Rubio
4 min readSep 19, 2020

--

Functions are really a hot topic in Python. For beginners of programming, is really hard to build complex functions because is needed a lot of practice to be able to use them in an easy way. In this article we will go through all the elements of a function, explaining first what a function is, why is needed and after we will go through the syntax, components, and types of functions.

Let’s get started!

What is a function?

A function is a set of statements that take inputs, to perform some specific tasks and gives back an output. Functions are extremely useful because when doing some commonly or repeatedly tasks, instead of writing the same code multiple times for different inputs, we can simply call the function. Understand the syntax of a function is pivotal, below you can see all its components:

  1. Keyword def that marks the start of the function header.
  2. A function name to uniquely identify the function. In the example above the name is calculate_years.
  3. Arguments through which we pass values to a function. They can be optional but in the function above I decided to add them(principal, interest, tax, desired).
  4. A colon (:) to mark the end of the function header.
  5. The function body, made by one or more valid python statements that must have the same indentation level.
  6. An optional return statement that returns a value from the function. In my example the return statement gives the following result:

Types of function?

Functions can be divided into the following three types:

  1. Built in Functions. Functions that are built into Python.
  2. User-defined-functions. Functions defined by the users.
  3. Anonymous Functions. A way to write throw-away functions on the fly.

The Built in Functions are “functions that are built into an application and can be accessed by end-users”. Their application is time-saving and the code can be much more elegant and understandable when using them. Below some examples of the most used Built in Function that every data scientist should know.

  • len(). The len() function takes one input and returns its length.
  • range(). A useful way to use the range() function is to simply pass it one whole number as an input. We’ll create a variable named school_mates and use range to fill that variable.
  • list(). We can use list() to list out the members in our range instance.

We can use list() with a for loop as follows:

  • min() and max(). They find the minimum and the maximum value of a list of numbers.

And the list of Built in Functions continues.

The User-defined-functions are functions defined by the users. The syntax of these functions has been already explained but it can be useful to see how they works with the following practical examples:

You can have the same output than above writing the function in a different way! Have a look:

Here is another example:

And you can have the same result by writing the function in a different way!

The Anonymous Functions are really useful as well! It is a short function having no more than one line and just like a normal function it can have multiple.

Another example!

The lamda function returned the product of all elements in a list.

Conclusion

Functions are the daily bread of a data scientist and it is fundamental to master it. I found really good websites in which I train myself but my favourite one is https://www.codewars.com/ ! Have a look at it and good training!

--

--