Jainam Shah
Jainam Shah

Reputation: 15

Are True and False considered respectively as 1 and 0 in python sets data structure?

In below code when False is already present in set_B, 0 is not printed and vice versa for True and 1. Why is this behavior only for sets?

set_B = {"HELLO", 23.435, "This is set B", 10, True}
set_B.update(range(0, 10))
print(set_B)
print(len(set_B))
print(0 in set_B)

output:

{0, True, 2, 3, 'This is set B', 4, 5, 6, 'HELLO', 7, 10, 8, 9, 23.435}
14
True

similarly,

set_B = {"HELLO", 23.435, "This is set B", 10, False}
set_B.update(range(0, 10))
print(set_B)
print(len(set_B))
print(0 in set_B)

output:-

{False, 1, 2, 'HELLO', 3, 4, 5, 6, 7, 8, 10, 9, 'This is set B', 23.435}
14
True

Upvotes: 1

Views: 35

Answers (1)

Austin
Austin

Reputation: 26037

Sets don't hold duplicates, hence it's a structure which is used where your entries are never repeated.

1 and 0 are analogous to True and False respectively.

You can confirm the same opening your terminal and executing below statements:

>>> 1 == True
True
>>> 0 == False
True

Upvotes: 1

Related Questions