Skip to content
Skip to main content

About this free course

Download this course

Share this free course

Learn to code for data analysis
Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

1.6 Functions

After the total and the average, next on my to-do list is to calculate the largest number of deaths.

An image with an angel in prayer statue in the foreground of a graveyard
Figure 5

This will be the maximum. It takes another single line of code to calculate it.

In []:

max(deathsInAngola, deathsInBrazil, deathsInPortugal)

Out[]:

6900

In this expression, max() is a function – the parenthesis are a reminder that the name max doesn’t refer to a variable. A function is a piece of code that calculates ( returns ) a value, given zero or more values (the function’s arguments ). In this case, max() has three arguments and returns the greatest of them. Actually, max() can calculate the maximum of two, three, four or more values.

In []:

max(deathsInBrazil, deathsInPortugal)

Out[]:

4400

The expressions above are function calls. I’m calling the max() function with three or two arguments, and the value of the expression is the value returned by the function. A function is called by writing its name, followed by the arguments, within parentheses and separated by commas. Function names follow the same rules as variable names.

As you might expect, Python also has a function to calculate the smallest (minimum) of two or more values.

In []:

min(deathsInAngola, deathsInBrazil, deathsInPortugal)

Out[]:

140

The value returned by a function call can be assigned to a variable. Here is an example, which calculates the range of deaths. The range of a set of values is the difference between the largest and the smallest of them.

In []:

largest = max(deathsInAngola, deathsInBrazil, deathsInPortugal)

smallest = min(deathsInAngola, deathsInBrazil, deathsInPortugal)

range = largest - smallest

range

Out[]:

6760

Exercise 3 Functions

Identify different types of error (some of you may have experienced those already…) in Exercise 3. You’ll need to use the Week 1 notebook to answer question three.

a. 

A syntax error


b. 

A name error


The correct answer is b.

b. 

The computer will understand than Min(…, …) is a function call but doesn’t know of any function with that name. Remember that names are case-sensitive.

Take a look at The art of naming.


a. 

A name error


b. 

A syntax error


The correct answer is b.

b. 

A function call requires two parentheses around the arguments, and one comma between successive arguments. Forgetting any of them therefore deviates from the syntax of the Python language.

Take a look at Functions.


a. 

4400


b. 

65480


c. 

240000


d. 

327400


e. 

235600


The correct answer is e.

e. 

The range is the maximum value (240 thousand for India) minus the minimum value (4400 for Brazil).