You've already seen and used functions such as print and abs. But Python has many more functions, and defining your own functions is a big part of python programming.

In this lesson, you will learn more about using and defining functions.

Getting Help

You saw the abs function in the previous tutorial, but what if you've forgotten what it does?

The help() function is possibly the most important Python function you can learn. If you can remember how to use help(), you hold the key to understanding most other functions.

Here is an example:

In [1]:

help(round)

`Help on built-in function round in module builtins:

round(number, ndigits=None) Round a number to a given precision in decimal digits.

The return value is an integer if ndigits is omitted or None.  Otherwise
the return value has the same type as the number.  ndigits may be negative.`

help() displays two things:

  1. the header of that function round(number, ndigits=None). In this case, this tells us that round() takes an argument we can describe as number. Additionally, we can optionally give a separate argument which could be described as ndigits.
  2. A brief English description of what the function does.

<aside> 💡 Common pitfall: when you're looking up a function, remember to pass in the name of the function itself, and not the result of calling that function.

</aside>

What happens if we invoke help on a call to the function round()? Unhide the output of the cell below to see.

In [2]:

help(round(-2.01))

unfold_more Show hidden output.