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.

Free course

Learn to code for data analysis

1.6 Functions

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

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.

1. If the function name is misspelled as Min, what kind of error is it?

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.


2. If a parenthesis or comma is forgotten, what kind of error is it?

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.


3. Use Exercise 3 in the Week 1 exercise notebook to answer this question.

What is the range of deaths among the BRICS countries (Brazil, Russia, India, China, South Africa)?

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).


LCDAB_1

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to university level study, find out more about the types of qualifications we offer, including our entry level Access courses and Certificates.

Not ready for University study then browse over 900 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus