>>> students_in_108
{'Paul Bohall', 'Raz Pi', 'Jason Jones'}
>>> students_in_108.discard('Alan Griffith')
>>> students_in_108.remove('Otis McCallum')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Otis McCallum'
>>>
By the Way: No En Masse
You cannot remove or discard en masse, as you can with the update operation. You
have to remove or discard set elements one at a time.
Programming with Sets
In this section, you’ll do some more weather temperature research—this time using sets. Using record
high temperatures (Fahrenheit) in May in Indianapolis, you will build two sets and then do some
analysis on them using a few set operations.
To build the first temperature set, highMayTemp2012, the set is initialized and then populated via
a for loop. This is shown in Listing 9.28, which displays part of the Python script
script0904.py.
LISTING 9.28 Populating a Set with the script0904.py Script
Click here to view code image
1: pi@raspberrypi ~ $ cat py3prog/script09024.py
...
2: # Populate set with High Temps (F) during May 2012 in Indianapolis
3: print ()
4: print ("Enter the high temps (F) for May 2012 in Indianapolis...")
5: #
6: highMayTemp2012 = set() #Create empty set
7: #
8: for may_date in range (1, 31 + 1): #Loop to enter temps
9: #
10: # Obtain high temp for date
11: prompt = "High temperature (F) May " + str(may_date) + " 2012: "
12: high_temp = int(input(prompt))
13: # Put element in set
14: highMayTemp2012.add(high_temp)
15: #
16: print ()
17: print ("The high temperatures (F) for May 2012 in a set are:")
18: print (highMayTemp2012)
19: #
...
Did You Know: A Camel in the Case
In Python script script0904.py, the set name highMayTemp2012 is using a