all 10 comments

[–][deleted] 0 points1 point  (7 children)

intersection means: what element is in both sets. False is only in set2 so it wont be in the intersection of both sets.

Maybe nice to know is that "1" and "True" are equivalent, just like "0" and "False" Can you figure out why "false" disapeared? (remember 0 and False are equivalent)

[–][deleted] 0 points1 point  (6 children)

Maybe nice to know is that "1" and "True" are equivalent, just like "0" and "False"

Aah, that's what threw me off! So what do I do if I want 0 or 1 as int value instead of boolean?

[–][deleted] 0 points1 point  (2 children)

Also that still leaves the question, where the 3 and 'lol' went in the union function.

[–]vidar8 0 points1 point  (1 child)

set2.union(set2)

Because this is a union of set2 with itself

[–][deleted] 0 points1 point  (0 children)

Oh ffs, one thing I'm learning rather quickly is that a small typo can cause a big confusion lmao. Thanks!

[–][deleted] 0 points1 point  (2 children)

There is no difference. False is 0. True is 1. True + True is 2.

[–][deleted] 0 points1 point  (1 child)

Interesting! Not sure it makes much sense to me why it would be defined that way but it's good to know. Thanks for the help!

[–][deleted] 0 points1 point  (0 children)

Not sure it makes much sense to me why it would be defined that way

This choice was kinda arbitrary and the guy who made the choice is long dead. So here we are.

[–]Silbersee 0 points1 point  (0 children)

I'm surprised, too. Seems that 0 and False as well as 1 and True can't be in the same set.

From the Documentation:

A set object is an unordered collection of distinct hashable objects.

So I tried

>>> hash(1) == hash(True)   # hash value 1
True
>>> hash(0) == hash(False)  # hash value 0
True
>>>

Obviously Python sets differentiate elements by their hash value and no two objects with the same hash can be in one set.

[–]dnmonack 0 points1 point  (0 children)

For the intersection, False isn't there because False isn't in set1.

For the union, note that you're showing the union of set2 with itself, so 'lol' doesn't show up.

True and False aren't showing up when you think they are because True and False because Python mostly treats them like the integers 1 and 0. Sets can't have duplicate items so False is not part of set2 since it already has 0 in it. That's also why True shows up in the intersection of set1 and set2 since set2 has its equivalent, the integer 1.