DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Set in Python (2)

Buy Me a Coffee

*Memo:

A non-empty set and empty set are:

  • True and False, checking them with bool() respectively.
  • False and True, inverting their truth values with not respectively.
# Non-empty set
print(bool({0}))
print(bool({frozenset()}))
# True

# Empty set
print(bool(set()))
# False
Enter fullscreen mode Exit fullscreen mode
# Non-empty set
print(not {0})
print(not {frozenset()})
# False

# Empty set
print(not set())
# True
Enter fullscreen mode Exit fullscreen mode

A set can be checked if a specific element is and isn't in the set with in and not in 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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

A set can be checked if the set is and isn't referred to by two variables with is and is not respectively as shown below:

*Memo:

  • Set literals with is and is not 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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

A set and other set can be checked if all the elements in:

  • them are and aren't equal with == and != respectively:
    • == and != can also check if types of values are and aren't the same respectively:
      • A set and frozenset and vice versa are True even though their types are different.
  • the set are in other set with <=.
  • the set and other elements are in other set with <.
  • other set are in the set with >=.
  • other set and other elements are in the set with >.
A = {10, 20, 30}

print(A == {10, 20, 30})       # True
print(A == {30, 20, 10})       # True
print(A == {10, 20, 40})       # False
print(A == {10, 20, -40})      # False
print(A == {10, 20})           # False
print(A == {10, 40})           # False
print(A == {10, -40})          # False
print(A == {10, 20, 30, 40})   # False
print(A == {10, 20, 40, 50})   # False
print(A == {10, 20, -40, -50}) # False
print(A == set())              # False

             # list
print(A == [10, 20, 30])            # False
print(A == frozenset([10, 20, 30])) # True
                 # frozenset
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A != {10, 20, 30})       # False
print(A != {30, 20, 10})       # False
print(A != {10, 20, 40})       # True
print(A != {10, 20, -40})      # True
print(A != {10, 20})           # True
print(A != {10, 40})           # True
print(A != {10, -40})          # True
print(A != {10, 20, 30, 40})   # True
print(A != {10, 20, 40, 50})   # True
print(A != {10, 20, -40, -50}) # True
print(A != set())              # True

             # list
print(A != [10, 20, 30])            # True
print(A != frozenset([10, 20, 30])) # False
                 # frozenset
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A <= {10, 20, 30})       # True
print(A <= {30, 20, 10})       # True
print(A <= {10, 20, 40})       # False
print(A <= {10, 20, -40})      # False
print(A <= {10, 20})           # False
print(A <= {10, 40})           # False
print(A <= {10, -40})          # False
print(A <= {10, 20, 30, 40})   # True
print(A <= {10, 20, 40, 50})   # False
print(A <= {10, 20, -40, -50}) # False
print(A <= set())              # False
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A < {10, 20, 30})       # False
print(A < {30, 20, 10})       # False
print(A < {10, 20, 40})       # False
print(A < {10, 20, -40})      # False
print(A < {10, 20})           # False
print(A < {10, 40})           # False
print(A < {10, -40})          # False
print(A < {10, 20, 30, 40})   # True
print(A < {10, 20, 40, 50})   # False
print(A < {10, 20, -40, -50}) # False
print(A < set())              # False
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A >= {10, 20, 30})       # True
print(A >= {30, 20, 10})       # True
print(A >= {10, 20, 40})       # False
print(A >= {10, 20, -40})      # False
print(A >= {10, 20})           # True
print(A >= {10, 40})           # False
print(A >= {10, -40})          # False
print(A >= {10, 20, 30, 40})   # False
print(A >= {10, 20, 40, 50})   # False
print(A >= {10, 20, -40, -50}) # False
print(A >= set())              # True
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A > {10, 20, 30})       # False
print(A > {30, 20, 10})       # False
print(A > {10, 20, 40})       # False
print(A > {10, 20, -40})      # False
print(A > {10, 20})           # True
print(A > {10, 40})           # False
print(A > {10, -40})          # False
print(A > {10, 20, 30, 40})   # False
print(A > {10, 20, 40, 50})   # False
print(A > {10, 20, -40, -50}) # False
print(A > set())              # True
Enter fullscreen mode Exit fullscreen mode

A set and other set can be checked if they have and don't have their common elements with bool() and & and with not 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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)