+ 1

Set difference Why set second - first is 8,7,9 not 7,8,9

first = {1, 2, 3, 4, 5, 6} second = {4, 5, 6, 7, 8, 9} print(first | second) print(first & second) print(first - second) print(second - first) print(first ^ second)

26th Jun 2021, 11:48 AM
Zahed Shaikh
Zahed Shaikh - avatar
4 Answers
+ 5
Zahed Shaikh This little code uses the random nature of SET to create a random number. I am hoping that it may help you understand the nature of set https://code.sololearn.com/c71Ipk1X1Mqq/?ref=app
26th Jun 2021, 11:58 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Zahed Shaikh Sets are not ordered. If you were to run your code multiple times, you will probably see a mixed ordered output
26th Jun 2021, 11:50 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
So set is random number generator
26th Jun 2021, 12:01 PM
Zahed Shaikh
Zahed Shaikh - avatar
+ 1
Not quite! Set stores the values in a random manner, which means you can't use an index to call a specific value. The code I showed puts a set of a range of numbers into a list. The set stores the range in a random manner. By putting it into a list, I can now call a specific index from the list
26th Jun 2021, 12:07 PM
Rik Wittkopp
Rik Wittkopp - avatar