Running this code multiple times
t = {'a', 'b', 'c', 'd'}
print(t)
could print something like:
{'c', 'b', 'a', 'd'}
{'d', 'b', 'c', 'a'} # different
{'d', 'b', 'c', 'a'} # same
{'a', 'd', 'b', 'c'} # different
{'a', 'b', 'c', 'd'} # different
# etc
(If you are using the console to replicate it, make sure you click Rerun every time before you re-paste the code and execute it. If you still can't replicate, perhaps you have hash randomization not equal to random
. On Python 3.3 and greater, hash randomization is turned on by default.)
On the other hand, the code below always prints the same set, and it's actually sorted:
s = {1, 6, 3.3, 4}
print(s)
# prints:
# {1, 3.3, 4, 6}
# {1, 3.3, 4, 6}
# {1, 3.3, 4, 6}
# {1, 3.3, 4, 6}
Question:
Why do sets of numbers appear to be always sorted and are they really always sorted?