diff --git a/native-datatypes.html b/native-datatypes.html index 901ec49..11da33f 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -597,33 +597,32 @@ KeyError: 'pop from an empty set'

Common Set Operations

-

FIXME +

Python’s set type supports several common set operations.

 >>> a_set = {2, 4, 5, 9, 12, 21, 30, 51, 76, 127, 195}
 >>> 30 in a_set                                                     
 True
->>> 31 in a_set                                                     
+>>> 31 in a_set
 False
 >>> b_set = {1, 2, 3, 5, 6, 8, 9, 12, 15, 17, 18, 21}
->>> a_set.union(b_set)                                              
+>>> a_set.union(b_set)                                              
 {1, 2, 195, 4, 5, 6, 8, 12, 76, 15, 17, 18, 3, 21, 30, 51, 9, 127}
->>> a_set.intersection(b_set)                                       
+>>> a_set.intersection(b_set)                                       
 {9, 2, 12, 5, 21}
->>> a_set.difference(b_set)                                         
+>>> a_set.difference(b_set)                                         
 {195, 4, 76, 51, 30, 127}
->>> a_set.symmetric_difference(b_set)                               
+>>> a_set.symmetric_difference(b_set)                               
 {1, 3, 4, 6, 8, 76, 15, 17, 18, 195, 127, 30, 51}
    -
  1. FIXME -
  2. -
  3. -
  4. -
  5. -
  6. +
  7. To test whether a value in a member of a set, use the in operator. This works the same as lists. +
  8. The union() method returns a new set containing all the elements that are in either set. +
  9. The intersection() method returns a new set containing all the elements that are in both sets. +
  10. The difference() method returns a new set containing all the elements that are in a_set but not b_set. +
  11. The symmetric_difference() method returns a new set containing all the elements that are in exactly one of the sets.
-

FIXME +

Three of these methods are symmetric.

 # continued from the previous example
@@ -638,14 +637,14 @@ KeyError: 'pop from an empty set'
>>> b_set.difference(a_set) == a_set.difference(b_set) False
    -
  1. FIXME -
  2. -
  3. -
  4. -
  5. +
  6. The symmetric difference of a_set from b_set looks different than the symmetric difference of b_set from a_set, but remember, sets are unordered. Any two sets that contain all the same values (with none left over) are considered equal. +
  7. And that’s exactly what happens here. Don’t be fooled by the Python Shell’s printed representation of these sets. They contain the same values, so they are equal. +
  8. The union of two sets is also symmetric. +
  9. The intersection of two sets is also symmetric. +
  10. The difference of two sets is not symmetric. That makes sense; it’s analogous to subtracting one number from another. The order of the operands matters.
-

FIXME +

Finally, there are a few questions you can ask of sets.

 >>> a_set = {1, 2, 3}
@@ -654,15 +653,15 @@ KeyError: 'pop from an empty set'
True >>> b_set.issuperset(a_set) True ->>> a_set.add(5) ->>> a_set.issubset(b_set) +>>> a_set.add(5) +>>> a_set.issubset(b_set) False >>> b_set.issuperset(a_set) False
    -
  1. FIXME -
  2. -
  3. +
  4. a_set is a subset of b_set — all the members of a_set are also members of b_set. +
  5. Asking the same question in reverse, b_set is a superset of a_set, because all the members of a_set are also members of b_set. +
  6. As soon as you add a value to a_set that is not in b_set, both tests return False.

Sets In A Boolean Context