Skip to main content
Data Science Wizardry Blog by Attila Vajda

Sunshine, blues day, jazz riffs and singing

I am revisiting the code puzzles I struggled with in the Coffee Break Python puzzle books, because I would like to become proficient at solving them.

I don't understand why these two sets are not equal, but I am trying to figure it out. Before I pasted this code snippet into the python3 console, I guessed that set([]) >= set([]) compares cardinality.

>>> marshmallow = [1, 1, 1, 2, 3, 3]
>>> gummy_bear = [0, 0, 0, 1, 3, 3]
>>> print(set(marshmallow) >= set(gummy_bear))
False
>>> set(marshmallow)
{1, 2, 3}
>>> set(marshmallow) > set(gummy_bear)
False
>>> set(marshmallow) < set(gummy_bear)
False
>>> set(marshmallow) = set(gummy_bear)
  File "<stdin>", line 1
SyntaxError: cannot assign to function call
>>> set(marshmallow) == set(gummy_bear)
False

Wow, this is awesome! >= means superset, so A >= B if B is superset of A

I was a bit confused, but I learned, that < denotes a proper subset. Set B is a proper subset of Set A, it means that all elements of B are also elements of A, but A contains at least one more element than A.

>>> A = {0, 1, 2}
>>> B = {0}
>>> B >= A
False
>>> B > A
False
>>> B < A
True
>>> B <= A
True
>>> A >= B
True
>>> set([1, 2]) < set([1 ,2, 3])
True
>>> set([1, 2, 3]) < set([1 , 2, 3])
False
>>> set([1, 2, 3]) >= set([1 ,2, 3])
True