Teaching material for Quantitative Methods track in Semester 1
Tutorial 1
Tutorial 2
Tutorial 3
Tutorial 4
Tutorial 5
Tutorial 6
Tutorial 7
Tutorial 8
In this tutorial we will examine different measures of location often referred to as averages. But before we do, first we will recap and take a more in-depth look at loops in Python.
A for
loop tells Python to execute some statements once for each value in a list, a character string, or some other iterable. Back in Tutorial 2 we introduced for
loops with a simple example of greeting a list of people by name:
names = ["Alice", "Bob", "Charlie", "Deborah"] # define a list of names
for name in names: # you can read this as "for each name in the list names"
print(f"Hello {name}") # python runs this code (the same line as before)
The essential ingredients of a for loop are a List, a loop variable, and a body:
ages = [7, 43, 18, 25] # The List: the items we loop over
for age in ages: # The Loop Variable: `age` is the loop variable
# The body: is in the next two lines of code and applies actions to
# the loop variable that is assigned to a value from the list.
age_in_five_years = age + 5
print(f'Someone who is {age} will be {age_in_five_years} in five years')
It is important to remember that the first line of the for
loop must end with a colon :
, and the body must be indented.
if
and else
conditional statements). Any consistent indentation will work, such as a Tab
or a set of Space
characters. A common practice is to use four spaces per indentation.You can repeat something a number of times using a list of numbers, e.g.,
for number in [0, 1, 2, 3, 4]: # for each number in this list
print(f"Hello {number}") # print hello
print(f"Goodbye {number}")
Instead of writing out a list of numbers you can use the Python function range()
Exercise 1: Try the example above, but replace the list [0, 1, 2, 3, 4]
with:
range(5)
range(10)
range(2,14)
range(2,14,2)
What is happening in each case? What does the range()
function do?
Exercise 2: Try these two versions of code:
for number in [0, 1, 2, 3, 4]: # for each number in this list
print(f"Hello {number}") # print hello
print(f"Goodbye {number}")
for number in [0, 1, 2, 3, 4]: # for each number in this list
print(f"Hello {number}") # print hello
print(f"Goodbye {number}")
How is the output different? Can you see why?
Exercise 3: Use the range()
function to create a for
loop that outputs the name and age of each person using two lists:
names = ["Alice", "Bob", "Charlie", "Deborah"]
ages = [7, 43, 18, 25]
For example, the output in each iteration of the loop should be something like Alice is 7 years old
.
Remember that you can index lists using square brackets []
, e.g., names[0]
returns the first name in the names
list.
Now we will use a for
loop to improve our game of Rock, Paper, Scissors that we started in Tutorial 3. Often when we play Rock, Paper, Scissors we play a series of rounds, e.g., best of 3 or best of 5.
Exercise 4: Use a variable n_rounds
to set a number of rounds to play and a for
loop to play Rock, Paper, Scissors n_rounds
times. Keep score and output the number of wins that the computer and the user have at the end of the game. Use an appropriate visualisation to summarise the number of wins by the computer and user.
Using a for
loop we can repeat a set of actions either a predetermined number of times (i.e., using range()
) or once per element in a list. However sometimes we would like to repeat a set of actions until a specific condition is satisfied.
In this case we can use a while
loop. The while
loop repeats until a condition is no longer satisfied. We can specify a condition in a similar way to if
statements. For example, we can create a while
to count until it reaches 5
.
count = 1
while count <= 5:
print(count)
count = count + 1
Just like a for
loop, a while
loop has a loop variable and a body, but instead of a list it has a conditional statement. A while
loop will continue to repeat while the conditional statement is True
and terminates when the conditional statement is False
.
Here is some code that asks a user to solve a simple maths problem that has two solutions.
answer = 0
attempts = 0
while answer not in [2, 4]:
answer = int(input('Enter an even number between 1 and 5'))
attempts = attempts + 1
print(f'Well Done! Correct after {attempts} attempts')
Exercise 5: Try the example above in your notebook and try to work out what each line of code does. Include your explanation in your notebook.
At the moment your game of Rock, Paper, Scissors will accept invalid or misspelled inputs from the user. We can use a while
loop to ask the user to input something until a valid input is given.
Exercise 6: Use a while
loop to check that a user correctly inputs either Rock, Paper or Scissors and ask for another input in the case they do not enter a valid input.
Exercise 7: Calculate the interest on a loan. Let’s say you want to borrow 100 Euros from a friend. Your friend agrees, but says they will charge you 1% interest per day until you pay them back.
First let’s calculate the amount you owe them after one day:
percentage_per_day = 1.0
loan = 100 # the amount you owe
interest = loan * percentage_per_day / 100 # this calculate the amount of interest after one day
loan = loan + interest # this is the new amount that you owe your friend after one day
print(f"You owe {loan} Euros")
Use a For
loop to work out how much would you owe your friend if you waited one year to pay them back?
Exercise 8: Use a while
loop to determine how long it takes to repay the loan?
Compound interest isn’t magic1 |
Averages tell us something about typical or usual values.
Mean: The mean, commonly known as the average, is a way to find the typical value in a set of numbers. To calculate it, you add up all the numbers and then divide by the total count of numbers. Essentially, the mean represents a balance point within the data, where the sum of all values is evenly distributed.
Mode: The mode is the value that appears most frequently in a dataset. It identifies the item or number that occurs with the highest frequency. The mode helps pinpoint the most common or popular element within the data, making it useful for finding the dominant choice or characteristic in a set of observations. It’s a straightforward way to understand what’s most typical in the data.
Median: The median represents the middle value when a set of numbers is sorted in ascending or descending order. If there’s an even number of values, the median is the average of the two middle values. It offers insight into the middle point of the data distribution, where half of the values are greater and half are lesser. Unlike the mean, the median isn’t influenced by extreme values, making it a robust measure for finding the central position or typical value in a dataset.
Read the data from the introduction survey into a variable gs_intro_survey
as you did in Tutorial 4.
url = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTEX2jqDTG_1uLK6lc1jxhsLQd47DSpVDOQH4MOqk0LJXoDXxWvc68ozzUafm_LlDPUqwV6CHEc8AvO/pub?gid=392676485&single=true&output=csv'
Exercise 9: Select the column heights and assign the values to a variable, e.g., heights = gs_intro_survey['Height']
and write a for
loop to calculate the sum of the heights and count the number of responses.
Exercise 10: Describe how to calculate the mean in your own words. Use your answer from Exercise 9 to calculate the mean height.
Exercise 11: What is the mode? Write some Python code to determine the mode of students’ previous Python_experience and their Enthusiasm. What does this tell you about your cohort?
A very useful Python module for statistics is called numpy
. All popular Python modules have some documentation to tell you how to correctly use the functions in the module.
Exercise 12: Take a look at the Numpy documentation for calculating descriptive statistics. Can you identify the function for calculating the mean?
Python documentation can seem very technical and overwhelming. But don’t panic, with some practice you will be able to make some sense of it.
Exercise 13: Look at the Numpy documentation that describes the np.mean()
function. There is a lot there that will probably not make sense. Try to focus on what, if anything, looks familiar. Is there anything you can understand? Make a note in your notebooks.
Exercise 14: Try to use the np.mean()
function to calculate the mean and check your answer to Exercise 9. If you get stuck, then ask chatGPT for help in understanding how to use the function (remember to include the prompt, response and your interpretation if using chatGPT).
Exercise 15: Look at the np.percentile()
function. Try to work out how you can use it to calculate the median income.
Exercise 16: If you wanted to fairly distribute income, then which measure of central location might be useful? Try it out and show your answer.
Different averages, including some less well known ones that we almost certainly will not cover in this course.2 |
Investing by xkcd licensed under CC BY-NC 2.5 ↩ ↩2
Geothmetic Meandian by xkcd licensed under CC BY-NC 2.5 ↩