What are these set operations, and why do they give different results?
The set operations you have mentioned are:
^
- symmetric difference (XOR):
Return a new set with elements in either the set or other but not both.
Example: {'1', '2', '3'} ^ {'2', '3', '4'} = {'1', '4'}
|
- union (OR):
Return a new set with elements from the set and all others.
Example: {'1', '2', '3'} | {'2', '3', '4'} = {'1', '2', '3', '4'}
There are also other set operations in python:
&
- intersection (AND):
Return a new set with elements common to the set and all others.
Example: {'1', '2', '3'} & {'2', '3', '4'} = {'2', '3'}
-
- difference:
Return a new set with elements in the set that are not in the others.
Example: {'1', '2', '3'} - {'2', '3', '4'} = {'1'}
The order of precedence for these operations is -, &, ^, |
, so in your example, we first apply ^
:
>>> y^z{'a', 'c', 'e', 'f', 'g', 'h', 'i'}
And then |
:
>>> x|{'a', 'c', 'e', 'f', 'g', 'h', 'i'}{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
The different outputs you describe are actually the same set, as sets are not ordered.
>>> {'c', 'h', 'f', 'd', 'b', 'i', 'g', 'a', 'e'} == {'a', 'd', 'h', 'f', 'b', 'g', 'e', 'c', 'i'}True
Any order shown in the string representation of a set is an implementation detail and should not be relied upon as it will vary unpredictably, as you have found.