Exercise 2: Lets go to the desert

In this exercise we'll make a decision based on data that's not as obvious as "it's raining" from the previous exercise.

Lets say I'm planning a trip for next May to visit a friend in Phoenix, Arizona, USA. I've never been there and live nowhere near there, so I'd like help in making a decision as to what types of clothing to pack for a 1 week trip. Being an extreme introvert, I'd never consider asking my friend for suggestions...

Knowing the expected high temperature for Phoenix in May would be useful. I did some research and found that the average high temperature is 93.6 F. Put another way, across the past decades, Phoenix has had May high temperatures higher than 93.6 F, such as 100.0 F and also high temperatures lower than 93.6 F, such as 85.0 F.

While temperature is definitely important, it doesn't give us a complete sense of the Phoenix climate, such as precipitation. We now know Phoenix is quite warm, but is it humid like a jungle or dry like a desert? The average precipitation for Phoenix in May is 0.1 inches. Phoenix appears to be arid, like a desert.

By knowing only the average high temperature and average precipitation, you can conjure a mental image of the Phoenix weather. Odds are, I won't need a rain poncho or arctic coat. Maybe sunglasses and flip-flops.

The process we just performed can be organized like this:

Gather info (historical weather data)  →  Predict future (the likely temps and precipitation)  →  Make decision (take sunblock)

With Python

Before we can make interesting predictions and decisions, you have to get more familiar with Python. It will take practice and will be hard and confusing, but it will be worth it.

Open a new script in Canopy. Type in the following code exactly. Remember, do not copy and paste.

Note

For this and future exercises, I will not show you screenshots of my computer screen. Instead, I'll show you the code like below, which you will type in to your computer.

print "I expected the average temp in PHX in May to be 80.0 F"
print "Based on the historical average it is actually %s F" % 93.6
print "This is a difference of %s degrees" % (93.6 - 80.0)
# I sure was off in my original estimate!

print "Lets convert 93.6 degrees Fahrenheit to Celsius."
print "I looked up an equation."
print "It says take the Fahrenheit value and subtract 32"
print "then divide everything by 1.8."

# This is your first *real* equation in Python.
93.6 - 32 / 1.80

print "Hmmm, why did the answer not display on the screen?"
print "Oh, because I forgot to 'print' the answer. Silly me."
print 93.6 - 32 / 1.8

print "Hmm, now the answer displays, but it's wrong."
print "Aha, I forgot to use ('s and )'s to group the equation"
print "just like in algebra class!"
print (93.6 - 32) / 1.8

print ""    # What does this line do?

print "Convert our temperature back to Fahrenheit."
print "Starting with Celsius value, multiply by 1.8, and add 32."
print "Now converting 34.222 C to F: %s" % (34.22 * 1.8 + 32)
print "It matches (close enough)!"

Save this file as ex2-phoenix_weather.py. Then click Run.

(That _ character is called "underscore". Look up how to type it with your keyboard. It's very commonly used when writing code.)

What you should see

Here is what the output of that program would look like:

I expected the average temp in PHX in May to be 80.0 F
Based on the historical average it is actually 93.6 F
This is a difference of 13.6 degrees
Lets convert 93.6 degrees Fahrenheit to Celsius.
I looked up an equation.
It says take the Fahrenheit value and subtract 32
then divide everything by 1.8.
Hmmm, why did the answer not display on the screen?
Oh, because I forgot to 'print' the answer. Silly me.
75.8222222222
Hmm, now the answer displays, but it's wrong.
Aha, I forgot to use ('s and )'s to group the equation
just like in algebra class!
34.2222222222

Convert our temperature back to Fahrenheit.
Starting with Celsius value, multiply by 1.8, and add 32.
Now converting 34.222 C to F: 93.596
It matches (close enough)!

Study Drills

  • Look up historical weather data for someplace you may visit in the future. Before looking at the data, think to yourself what you'd expect the data to look like (high temperatures, amount of precipitation, etc.). Use http://climate-zone.com, http://weatherspark.com, or http://accuweather.com.
  • What if you and your friend want to camp in a tent? What do the nighttime temperatures look like? Most weather data sources don't provide "nighttime" temperatures. What data that is provided likely corresponds to nighttime temperatures? Average high temperature? Average low temperature? Something else?
  • In the code you typed above, what do you think the %s does? See if you can change what appears in the %s locations without modifying a %s.

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