2

I am a beginner in python. As a part of my learning over data types, I came across this particular behavior of SET

If I print a set as below

print ({2,1,3,4})

it giving sorted out put

{1, 2, 3, 4}

If I print as below

print ({7,6,8,9} | {1,2,3,4})

it giving again sorted out put only

{1, 2, 3, 4, 6, 7, 8, 9}

But if i print the below sets

print ({7,6,8,9})
print ({2,1,8,9})

the output I am receiving as below

{8, 9, 6, 7}
{8, 1, 2, 9}

which is not sorted

So please help me in understanding , on what basis this out put is coming.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • The output ordering of set elements is purely accidental. You could look into the Python interpreter source code to see why it behaves like this, but for all practical purposes, the order of elements in a set should be ignored. –  Apr 25 '18 at 06:12
  • @Evert Thanks for the suggestion.Going forward, will not consider the order. But still refer the source code to satisfy my curiosity. – akhilp narayanan pathirippilly Apr 25 '18 at 07:19

0 Answers0