class. The type function is then used on it. You can see that the students_in_108 is a set
object type.
LISTING 9.16 Creating an Empty Set
Click here to view code image
>>> students_in_108 = set()
>>> type (students_in_108)
<class 'set'>
>>>
Populating a Set
To add a single element to a set, you use the .add operation, which has the following syntax:
set_name.add (element)
To add the value elements to the students_in_108 set, you enter them one at a time and press
Enter after each, as shown in Listing 9.17.
LISTING 9.17 Populating a Set with the .add Operation
Click here to view code image
1: >>> students_in_108.add('Raz Pi')
2: >>> students_in_108.add('Jason Jones')
3: >>> students_in_108.add('Paul Bohall')
4: >>> students_in_108
5: {'Paul Bohall', 'Raz Pi', 'Jason Jones'}
6: >>>
To display the current elements in a set, you just type the set’s name, as shown on line 4 in Listing
9.17. You can see that the elements are unordered, as you would expect in a set.
Using a less tedious method, you can create and populate a set all in one command. To do so, you use
the following syntax:
Click here to view code image
set_name([element1, element2, ... elementn])
In Listing 9.18, a new set is created for the “133 Python Programming” class.
LISTING 9.18 Populating a Set with One Command
Click here to view code image
>>> students_in_133 = set(['Raz Pi', 'Benny Lora', 'Jody Sanchez'])
>>> students_in_133
{'Raz Pi', 'Benny Lora', 'Jody Sanchez'}
>>>