Exercise 4: Hobbits, Dwarves, and Humans

Below are data [1] acquired from The Lord of the Rings and other books about Middle Earth by J.R.R. Tolkien.

Race Life Expectancy (years)
Hobbits 96.0
Dwarves 194.0
Humans 163.0

If we assume that the population of Hobbits, Dwarves, and Humans is about equal, and we collectively refer to these three races as People, then what's the mean life expectancy of a Person? To calculate mean, sum all the values and then divide by how many values you summed.

mean = (sum of values)/(number of values summed)

For our Middle Earth data, the mean is calculated this way:

mean = (96.0 + 194.0 + 163.0)/(3) = (453.0)/(3) = 151.0  years

The average life expectancy of a Person is 151.0 years.

Statistics as the Cliff's Notes of data

Determining a mean is one example of how statistical tools provide us with the Cliff's Notes of data. By studying the birth and death dates of many Hobbits, Dwarves, and Humans, a researcher was able to determine the life expectancy -- or mean number of years of life -- for each race. Then we calculated the mean of the three means, further summarizing the data. It's much easier to understand a single number rather than trying to comprehend hundreds or thousands of birth and death dates mentioned throughout Tolkien's many books.

Warning

Always think about what a calculation tells you. For example:

  • most humans have 2 legs,
  • some have 1 leg,
  • some have 0 legs, and
  • no humans have 3 or more legs.

If you surveyed all the people in your town, you'd probably calculate the mean number of legs a human has as 1.999999999 legs. The mathematics is sound, but it'd be silly to expect to find a single human who has 1.999999999 legs. When summarizing a bunch of data, sometimes you uncover a result that describes not a single piece of the actual data.

For each calculation you perform in this book, check to see if your answer makes sense in the context of what you're studying. Always think:

Can a human have 1.9999999 legs?

What to type

Lets calculate the average life expectancy of the Middle Earth People in three different ways:

# Approach 1 - typing numbers without meaning
# Uses only math
print (96.0 + 194.0 + 163.0) / 3

# Approach 2 - applying labels to the numbers
# Uses variables and math
hobbits = 96.0
dwarves = 194.0
humans = 163.0
totalRaces = 3
mean = (hobbits + dwarves + humans) / totalRaces
print mean

# Approach 3 - adding context to the numbers
# Uses a list of numbers, variables, and some functions
lifeExpectancies = [96.0, 194.0, 163.0]
sumYears = sum( lifeExpectancies )
numberRaces = len( lifeExpectancies )
mean = sumYears / numberRaces
print mean

Save this file as ex4-life_expectancy.py. Then run it.

What you should see

You should get this:

151.0
151.0
151.0

While each approach gives the same result, each subsequent approach is more powerful, that is, more easily applied to much bigger and more interesting problems beyond just Hobbits, Dwarves, and Humans. However, you are likely concerned about the stuff that appears in Approach #2 and especially in Approach #3.

Lets dig into Approach #2:

# Approach 2 - applying labels to the numbers
# Uses variables and math
hobbits = 96.0
dwarves = 194.0
humans = 163.0
totalRaces = 3
mean = (hobbits + dwarves + humans) / totalRaces
print mean

In plain English, here is what the above code tells your computer to do:

  • Lines 1 and 2: Disregard these comments because they are for humans and you are a robot.
  • Lines 3-6: Put four numbers in four separate folders.
  • Line 7: Get all four folders that were just created, arrange their contents in a particular way, and then stuff the result into a fifth folder, labeled 'mean'.
  • Line 8: Display on the screen what is inside the 'mean' folder.

From now on, I will not pretend that variables are akin to physical folders. I'll just call them variables like everyone else. You may be comfortable with the concepts of variables, printing, and basic math in Python. If not, do not worry -- review the code, try changing a word here, a number there, and see what happens.

Read through Approach #3 now and see if you can guess what each line does. On a sheet of paper, write what you think is happening, line-by-line. It's okay if you don't understand everything -- there are new concepts here that you may have never seen before. If your eyes gloss over, skip it and go to the next exercise. You can come back later when you're more comfortable.

Study Drill

Search online for "python lists".

  • Read about what they look like and some ways to use lists.
  • If you see sample code, open a new window in IDLE and run the code to see what happens.

Challenge

If you think you understand Approach #3, try simplifying it. I can shorten it to just 2 lines of code. Hint: Try creating only a single variable to hold the data.

[1]The table does not include Elves or Ainur since they live forever unless killed. Also, there is much variation in the death dates of Dwarves due to battle and of Humans because there are several types of Humans, including First, Second, and Third Ages, as well as Númenóreans. Data table based on work by Emil Johansson of http://lotrproject.com.

Creative Commons License
Learn Stats in 10,000 Hours by Jonathan B. Miller is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
comments powered by Disqus