*Memo:
A non-empty set and empty set are:
-
TrueandFalse, checking them with bool() respectively. -
FalseandTrue, inverting their truth values withnotkeyword respectively.
# Non-empty set
print(bool({0}))
print(bool({frozenset()}))
# True
# Empty set
print(bool(set()))
# False
# Non-empty set
print(not {0})
print(not {frozenset()})
# False
# Empty set
print(not set())
# True
A set can be checked if a specific element is and isn't in the set with in keyword and with not and in keyword respectively as shown below:
A = {10, frozenset([20, 30])}
print(10 in A)
print({20, 30} in A)
# True
print(20 in A)
print(30 in A)
print({10} in v)
print({20} in v)
print({30} in v)
print({10, frozenset([20, 30])} in A)
print(set() in A)
# False
A = {10, frozenset([20, 30])}
print(10 not in A)
print({20, 30} not in A)
# False
print(20 not in A)
print(30 not in A)
print({10} not in A)
print({20} not in A)
print({30} not in A)
print({10, frozenset([20, 30])} not in A)
print(set() not in A)
# True
A set can be checked if the set is and isn't referred to by two variables with is keyword and with is and not keyword respectively as shown below:
*Memo:
- Set literals with
iskeyword and withisandnotkeyword don't get warnings respectively.
A = {10, 20, 30}
B = {10, 20, 30}
C = A
print(A is B) # False
print(A is C) # True
print(A is not B) # True
print(A is not C) # False
print({10, 20, 30} is {10, 20, 30}) # False
print({10, 20, 30} is {10, 20}) # False
print({10, 20, 30} is not {10, 20, 30}) # True
print({10, 20, 30} is not {10, 20}) # True
A set and other set can be checked if all the elements in:
- them are and aren't the same with
==and!=respectively. - the set are in other set with
<=. - other set are in the set with
>=. - the set and other elements are in other set with
<. - other set and other elements are in the set with
>.
A = {10, 20, 30}
print(A == {10, 20, 30}) # True
print(A == {10, 20, 40}) # False
print(A == {10, 20}) # False
print(A == {10, 40}) # False
print(A == {10, 20, 30, 40}) # False
print(A == {10, 20, 40, 50}) # False
print(A == set()) # False
A = {10, 20, 30}
print(A != {10, 20, 30}) # False
print(A != {10, 20, 40}) # True
print(A != {10, 20}) # True
print(A != {10, 40}) # True
print(A != {10, 20, 30, 40}) # True
print(A != {10, 20, 40, 50}) # True
print(A != set()) # True
A = {10, 20, 30}
print(A <= {10, 20, 30}) # True
print(A <= {10, 20, 40}) # False
print(A <= {10, 20}) # False
print(A <= {10, 40}) # False
print(A <= {10, 20, 30, 40}) # True
print(A <= {10, 20, 40, 50}) # False
print(A <= set()) # False
A = {10, 20, 30}
print(A >= {10, 20, 30}) # True
print(A >= {10, 20, 40}) # False
print(A >= {10, 20}) # True
print(A >= {10, 40}) # False
print(A >= {10, 20, 30, 40}) # False
print(A >= {10, 20, 40, 50}) # False
print(A >= set()) # True
A = {10, 20, 30}
print(A < {10, 20, 30}) # False
print(A < {10, 20, 40}) # False
print(A < {10, 20}) # False
print(A < {10, 40}) # False
print(A < {10, 20, 30, 40}) # True
print(A < {10, 20, 40, 50}) # False
print(A < set()) # False
A = {10, 20, 30}
print(A > {10, 20, 30}) # False
print(A > {10, 20, 40}) # False
print(A > {10, 20}) # True
print(A > {10, 40}) # False
print(A > {10, 20, 30, 40}) # False
print(A > {10, 20, 40, 50}) # False
print(A > set()) # True
A set and other set can be checked if they have and don't have their common elements with bool() and & and with not keyword and & respectively as shown below:
A = {10, 20, 30}
print(bool(A & {20, 40})) # True
print(bool(A & {40, 50})) # False
print(bool(A & set())) # False
print(not (A & {20, 40})) # False
print(not (A & {40, 50})) # True
print(not (A & set())) # True
A set cannot be enlarged with * and a number as shown below:
A = {10, 20, 30, 40, 50} * 3
# TypeError: unsupported operand type(s) for *: 'set' and 'int'
A set and other sets cannot be concatenated with + as shown below:
A = {10, 20, 30} + {30, 40} + {50, 60, 70, 80}
# TypeError: unsupported operand type(s) for +: 'set' and 'set'
A set and other set can return:
- all the elements in them with
'|'(Union: A ∪ B). - their common elements with
'&'(Intersection: A ∩ B). - the elements in the set which aren't in other set with
'-'(Difference: A - B). - the elements in either the set or other set but not both with
'^'(Symmetric Difference: A Δ B).
A = {10, 50}
B = {10, 30, 50}
C = {10, 20, 40, 50}
print(A | B)
# {50, 10, 30}
print(A | C)
# {50, 20, 40, 10}
print(B | C)
# {50, 20, 40, 10, 30}
print(A | B | C)
# {50, 20, 40, 10, 30}
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
print(A & B)
# {10, 30}
print(A & C)
# {40, 30}
print(B & C)
# {30}
print(A & B & C)
# {30}
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
print(A - B)
# {40, 20}
print(A - C)
# {10, 20}
print(B & C)
# {30}
print(A - B - C)
# {20}
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
print(A ^ B)
# {40, 50, 20}
print(A ^ C)
# {10, 20}
print(B ^ C)
# {40, 10, 50}
print(A ^ B ^ C)
# {50, 20, 30}
Top comments (0)