How come I can add the boolean value False but not True in a set in Python?

  • A+
Category:Languages

I just started investigating the set data type in Python. For some reason, whenever I add the Boolean value of True to a set it doesn't appear. However, if I add False to a set it will become an element of the set. I was shocked when I googled this question that nothing came up.

example1 = {1, 2, 7, False} example2 = {7, 2, 4, 1, True}  print(example1) print(example2) 

The output is:

{False, 1, 2, 7} {1, 2, 4, 7} 

 


Because in Python 1 == True and you have 1 in your set already.

Imagine this example:

example1 = {0, False, None} example2 = {1, True}  print(example1) print(example2) 

Will output:

{0, None} {1} 

First set has 0 and None because 0 == False but 0 != None. With second set 1 == True so True isn't added to the set.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: