6: diff_temps2011 = highMayTemp2011.difference(highMayTemp2012)
7: #
8: # Print out determined data
9: print ()
10: print ("Which month do you think was cooler?")
11: print ("May 2012:", sorted(diff_temps2012))
12: print (" or")
13: print ("May 2011:", sorted(diff_temps2011))
...
Two difference sets are built, as shown on lines 5 and 6 in Listing 9.30. Both of these difference sets
are then printed out, so the script user can determine which month was cooler.
Now that you have seen scripts0904.py’s construction, take a look at the output produced when
the script is run. Listing 9.31 shows the final results for the intersection and difference calculations
for the sets.
LISTING 9.31 Output of script0904.py
Click here to view code image
pi@raspberrypi ~ $ python3 py3prog/script0904.py
Enter the high temps (F) for May 2012 in Indianapolis...
High temperature (F) May 1 2012: 79
High temperature (F) May 2 2012: 84
High temperature (F) May 3 2012: 85
...
The high temperatures (F) for May 2012 in a set are:
{70, 72, 74, 75, 76, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 90, 91, 92}
...
The high temperatures (F) for May 2011 in a set are:
{65, 68, 69, 70, 71, 72, 74, 77, 79, 80, 82, 84, 85, 87, 88, 57, 90, 59, 60}
...
High Temps (F) Shared by May 2012 & May 2011
[70, 72, 74, 79, 80, 84, 85, 87, 88, 90]
Which month do you think was cooler?
May 2012: [75, 76, 78, 81, 83, 86, 91, 92]
or
May 2011: [57, 59, 60, 65, 68, 69, 71, 77, 82]
Notice that even though 31 days of data was entered, the sets are pretty small. Remember that each
data element in a set must be unique, so any duplicate temperatures are eliminated.
You have seen that using set mathematics can help you draw conclusions about data. According to the
Python script results, which month was cooler in Indianapolis: May 2012 or May 2011?
Summary
In this hour, you got to try out two data storage object types: dictionaries and sets. You learned how
to create both empty dictionaries and sets as well as how to populate them. Also, you were
introduced to concepts such as obtaining data from, updating, and managing both dictionaries and sets.
Finally, you saw some practical examples of using these data collection types in building a Python