>>> engineers = {'bob', 'sue', 'ann', 'vic'} >>> managers = {'tom', 'sue'}
>>> 'bob' in engineers True
>>> engineers & managers {'sue'}
>>> engineers | managers
{'bob', 'tom', 'sue', 'vic', 'ann'}
>>> engineers - managers {'vic', 'ann', 'bob'}
>>> managers - engineers {'tom'}
>>> engineers > managers False
# Is bob an engineer?
# Who is both engineer and manager?
# All people in either category
# Engineers who are not managers
# Managers who are not engineers
# Are all managers engineers? (superset)
>>> {'bob', 'sue'} < engineers True
>>> (managers | engineers) > managers True
>>> managers ^ engineers {'tom', 'vic', 'ann', 'bob'}
# Are both engineers? (subset)
# All people is a superset of managers # Who is in one but not both?
>>> (managers | engineers) - (managers ^ engineers) # Intersection! {'sue'}
Set comprehensions in Python 3.X and 2.7
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.