Something of type set is an unordered collection of objects. An element can only be a member of a given set once, so in that sense it is much like a mathematical set. In fact, that’s the point. Because a set is unordered operations, indexing and slicing are not provided. Python does support membership (is), size (len()), and looping on membership (for i in set).
Mathematical sets have certain specific, well-defined operations, and those are available on a Python set also.
Subset set1 < set2 means setl is a true subset of s2.
Intersection set1 & set2 creates a new set containing members in common with both.
Union set1 & set2 creates a new set with all elements of both.
Difference set1-set2 creates a new set with members that are not in both.
Equality set1==set2 is true if both sets contain only the same elements.
Creating a new object of type set is a matter of specifying either that it is a set or what the elements are. One way is to use the {} syntax:
setl = {1,3,5,7,9}
or to use the constructor
set2 = set(range(1, 10))
which gives the set {1, 2, 3, 4, 5, 6, 7, 8, 9}. Therefore,
setl<set2 is True
set1 & set2 is {9, 1, 3, 5, 7} (Note: Order does not matter to a set.)
set1 | set2 is {1, 2, 3, 4, 5, 6, 7, 8, 9}
set2 – set1 is {8, 2, 4, 6}
A new element can be added to a set using add():
set1.add(11)
and removed using remove():
set1.remove(11)
or discard():
set1.discard(11)
If the element being removed is not in the set, then an error will occur (Key- Error) when remove() is called, but not with discard(). This should be tested first or be placed in an except statement.
All of the examples so far involve integers belonging to a set, but other types can belong as well: floating point numbers, strings, and even tuples (not lists). For example, the following are legal sets:
{“a”, “e”, “i”, “o”, “u”}
{“cyan”, “yellow”, “magenta”}
{(2,4), (3,9), (4,16), (5,25), (6,36), (7,49)}
1. Example: Craps
Craps is a dice game, and it commonly involves betting on the outcome. The player (shooter) rolls two dice. If, on the first roll (pass), a total of 7 or 11 is obtained, then the shooter wins. An initial roll of 2, 3, or 12 loses immediately. Any other roll is called the point. In that case, the shooter continues to roll the dice. If a 7 is obtained, then the shooter loses, and if the point number is rolled, then the shooter wins. The shooter continues to roll until on or the other occurs. One way to implement this game in Python is to use sets.
Elements of the sets are the values on each die, which is to say one roll. There are two dice, so a total of 36 combinations exist. A single roll is a tuple, such as (1,1) or (3,4). There are only 12 distinct sums of two dice, and multiple ways to achieve them. A sequence named roll is created that contains a set for each possible value, and that set contains all of the ways that the value can be obtained. For instance, there are two ways to roll a 3, so
roll[3] = {(1,2), (2,1)}
Initially, a set is created for each possible roll of a pair of dice and then is initialized as described:
Now roll[i] contains all of the ways to roll a value of i. In particular, roll[7] contains all ways to roll a 7 and roll[11] contains all ways to roll an 11. Thus, all of the rolls that win on the first pass can be placed in a single set, the union of roll[7] and roll[11]:
winner = roll[7] | roll[11]
Similarly, the rolls that will lose for the shooter on the first pass are as follows:
loser = roll[2] | roll[3] | roll[12]
If any other roll is thrown, then that becomes the point. Roll the die amount to get a random number between 1 and 6, inclusive, or
die1 = randrange(1,7)
die2 = randrange(1,7)
Remember that randrange() produces a number less than the second parameter. Given this roll, the point is the set roll[die1+die2]. Continuing the program from the die rolls:
Now the dice are rolled repeatedly. If the roll is in the point set, then the shooter wins. If the roll is a 7 (in the set roll[7]), then the player loses. Otherwise the shooter rolls again.
In a real craps game, this entire process is repeated, and bets are placed on each individual game as to whether the player will win or lose.
Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.