>>> numbs = {1, 2, 4, 5, 6, 8, 10}
>>> odds = {1, 3, 5, 7, 9}
To check that these are indeed sets, use the type()
function:
>>> type(odds)
<class 'set'>
To join two sets (just as in a mathematical join), you can
use the | operator to show a combined set with no
duplicates:
Click here to view code image
>>> numbs | odds
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
You can get an intersection of two sets and show what
numbers are in both by using the & operator:
>>> numbs & odds
{1, 5}
There are many ways to evaluate sets, and the Python
documentation can be used to explore them all. In
addition, Python has library collections that give you
even more options for ways to store data. Search the
Python documentation for “collections” and take a look
at ordered dictionaries, named tuples, and other
variations that may suit your data collecting needs. For
the purposes of the DEVASC exam, though, you do not
need to know data structures beyond the basic types
discussed here.
INPUT AND OUTPUT