[Tutor] Preference to Bitwise operators in comparison to Logical operators

Alan Gauld alan.gauld at yahoo.co.uk
Fri Sep 11 04:11:28 EDT 2020


On 11/09/2020 05:00, David wrote:
> On Fri, 11 Sep 2020 at 07:55, Manprit Singh <manpritsinghece at gmail.com> wrote:
> 
>> As we all know True is 1 and False is 0 .
> 
> Not in Python ...
> 
> $ python3
> Python 3.7.3 (default, Jul 25 2020, 13:03:44)
> [GCC 8.3.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> True is 1
> False
>>>> False is 0
> False
>>>>

Except
>>> True == 1
True
>>> False == 0
True
>>> True == 2
False

>>> True + True
2
>>> True - True
0
>>> False + False
0
>>> False * True
0

So when used in a numeric context they are equal to 1 and 0.
They are just distinct objects from the integer singletons.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list