Sets are unordered, so there is no difference between {False, True}
and {True, False}
. Either rendering is "correct", and you shouldn't build any assumptions around any particular ordering (even if in practice you see it rendered in sorted order -- AFAIK this is a property of the standard CPython implementation but is not a feature of the language, so it's subject to change without notice).
>>> {False, True} == {True, False}
True
>>> {False, True}
{False, True}
>>> {True, False}
{False, True}
You probably don't want to be returning a set from this function in the first place. Try using the all
function to turn your generator expression into a single bool:
>>> def is_prime(number):
... return all(number % i for i in range(2, number))
...
>>> is_prime(15)
False
>>> is_prime(7)
True
This produces equivalent results to if you were to test that your set
only contains True
:
>>> def is_prime(number):
... return {True} == set(([True if number % i != 0 else False for i in range(2, number)]))
but is obviously much less code, and it also runs more quickly because it's not necessary to build the entire list in memory and turn it into a set; the all
function stops reading values from the generator as soon as it hits the first False
value.