 # Show 04: Hobbits

Below are data  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.

\begin{equation*} \text{mean} = \frac{\text{sum of values}}{\text{number of values summed}} \end{equation*}

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

\begin{equation*} \text{mean} = \frac{96.0 + 194.0 + 163.0}{3} = \frac{453.0}{3} = 151.0 \text{ years} \end{equation*}

The average life expectancy of a Person is 151.0 years.

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

  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.