a=[0, True, False, 1]
a.sort()
max(a)
min(a)
After sorting, the value of a becomes [0, False, True, 1]. On what basis, python sorts the original list like this when it is said that True is equal to 1 nd False is equal to 0.
- max(a) gives True as the result
- min(a) gives 0 as the result
How does Python treat boolean True as bigger than 1? How does Python treat 0 as less than False?
Unable to comprehend how python compares boolean values with integers.
Python does not treat
Trueas larger than 1. It just returns the first one if there are multiple maximal elements. Quote from the official documentation:The same goes for
min().max()returnsTruebecause it is equal to1and comes earlier in your input, not because somehowTrueis larger than1. This is evidenced by that if you switch1andTrueina, you get1asmax(a).list.sort()in python is stable.0andFalsecompare equal, but0comes beforeFalsein the originala, so aftera.sort(),0is ordered beforeFalse. Same reasoning for whyTruecomes before1.