Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python max() and min() values for bool

Tags:

python

In python interpreter,

min(True,False)==False
max(True,False)==True

is assured by design?

like image 632
Alfad Avatar asked Nov 28 '25 23:11

Alfad


2 Answers

True is equal to 1 and False is 0

like image 106
jamylak Avatar answered Dec 01 '25 13:12

jamylak


It seems, at least in CPython, bool subclasses int. Therefore, you can do:

>>> abs(False)
0
>>> abs(True)
1

and:

>>> False < True
True
>>> True > False
True

I guess max and min work on the comparison operator:

>>> cmp(False, True)
-1
>>> cmp(True, False)
1
>>> cmp(False, False)
0
>>> cmp(True, True)
0
like image 39
Daren Thomas Avatar answered Dec 01 '25 13:12

Daren Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!