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

Richard Damon Richard at Damon-Family.org
Fri Sep 11 20:25:09 EDT 2020


On 9/11/20 4:11 AM, Alan Gauld via Tutor wrote:
> 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.
>
So True == 1, but True is not 1

Basically, Python defines a special relationship called "is".

Note, that for many numeric values, you can get two values that compare
equal that might not compare with each other with "is" because "is"
tests object identity, and numbers are not singletons (where all
instances of the same value use the same object).

In many cases "small" numbers are a special case, and are combined for
efficiency, but this is not guaranteed.

-- 
Richard Damon



More information about the Tutor mailing list