Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
variable style of naming called camel Case. camel Case variable names, popular with
Python script writers, start out with lowercase, and then subsequent words in the name
start with uppercase characters. This helps to add clarity to a script. And, supposedly,
the name looks like a camel with several humps.

Nothing is too exciting in script094.py so far. You have seen how to gather data into a set
before. In this script, a second set, not shown in Listing 9.28, is also built just like the first one,
except the set name is highMayTemp2011.


By the Way: Actual Temperature Data
The temperature data used in this hour is actual May temperatures for Indianapolis,
Indiana. It was derived from http://www.almanac.com/weather/history/IN/Indianapolis.

Now that the necessary data is loaded into the scripts, you can do a little set mathematics to provide
some analysis. First, you can compare the 2012’s and 2011’s May high temperatures by using a set
intersection. The intersection should show the May high temperatures shared by both years. In Listing
9.29, the Python statement needed to accomplish this set intersection is shown on line 5.


LISTING 9.29 Setting an Intersection with the script0904.py Script


Click here to view code image


1: pi@raspberrypi ~ $ cat py3prog/script09024.py
...
2: # Determine Shared High Temps for May
3: #
4: # Find intersetion of high temp sets
5: shared_temps = highMayTemp2011.intersection(highMayTemp2012)
6: #
7: # Print out determined data
8: print ()
9: print ("High Temps (F) Shared by May 2012 & May 2011")
10: print (sorted(shared_temps))
...

Set mathematics performed on the temperature data also allows you to see which month was cooler
(May 2011 or May 2012). To accomplish this, a set difference must be performed on the data. Listing
9.30 shows the code from scrip0904.py, which performs a set difference.


LISTING 9.30 Setting a Difference with the script0904.py Script


Click here to view code image


1: pi@raspberrypi ~ $ cat py3prog/script09024.py
...
2: # Determine Which Month was Cooler - May 2011 or May 2012
3: #
4: # Find difference of high temp sets
5: diff_temps2012 = highMayTemp2012.difference(highMayTemp2011)
Free download pdf