Subtracts a boolean from a float in python

In any case, when debugging my code, I found a statement that basically subtracted boolean from the float .

Then I tried to execute in python console:

 >>> 15.0 - True 14.0 >>> 15.0 - False 15.0 

Can someone explain to me:

+4
source share
3 answers
  • http://docs.python.org/3/library/stdtypes.html#boolean-values

    Boolean values โ€‹โ€‹are two constant objects False and True. They are used to represent truth values โ€‹โ€‹(although other values โ€‹โ€‹may also be considered false or true).

    In numerical contexts (for example, when used as an argument to an arithmetic operator), they behave as integers 0 and 1, respectively.

  • not at present but you can write

    result = numeric_value * a_bool (this is often used, for example, in shader languages)

    instead

    result = numeric_value if a_bool else 0

    or

    result = (value_if_false, value_if_true)[a_bool]

    Don't do it though.

In most cases, people with experience in lower-level languages โ€‹โ€‹expect to be taken away. In C, true and false are still macros for 1 and 0.

Prior to version 2.3, Python did not have a bool type, so when it was introduced by making it a subclass of int , make sure that the code was not broken.

+4
source

This is legal because bool is a subclass of int :

 >>> bool.__bases__ (<type 'int'>,) >>> True == 1 True >>> False == 0 True 

Yes, it has a practical use. For example, it was possible to do something similar before the triple statement was introduced:

 result = [value_if_false, value_if_true][condition] 

Which basically does what will be done in this code:

 if condition: result = value_if_false else: result = value_if_true 

Other practical exercises:

  • you can sum multiple checks / booleans get the number of results equal to True ,
  • you can multiply the result of the check:

     result = can_count_a * a + can_count_b * b 
+6
source

True evaluates to 1, and False evaluates to 0.

 >>> True is 1 False >>> True == 1 True >>> 

Bool is a subclass of int. As indicated in PEP-285 :

6) Should bool inherit from int?

=> Yes.

In an ideal world, bool could be better implemented as a separate integer type that knows how to perform mixed arithmetic. However, bool inheritance completely excludes the implementation (in part, since all C code that calls PyInt_Check () will continue to work - this returns true for subclasses of int). In addition, I think this is correct in terms of interchangeability: code that requires an int can be loaded by bool, and it will behave the same as 0 or 1. Code that requires bool to not work when it is given an int; for example, 3 & 4 is 0, but both 3 and 4 are true if we count the truth values.

This is not very practical, and there are other answers with sudo examples of using bools. I thought it would be nice to have some real examples:

 f,b="Fizz","Buzz" print "\n".join([["",f,b,f+b][(x%3==0) + 2*(x%5==0)] or str(x) for x in range(1,101)]) 

This section:

 ["",f,b,f+b][(x%3==0) + 2*(x%5==0)] 

The choice of returning each line is based on two Boolean expressions, if both of them are true, we get (True) + 2*(True) , which evaluates to 4, which is fizzbuzz. It's not too hard to understand once you get used to the idea that True == 1 and False == 0

Further compliance with the topic:

 print '\n'.join(['Fizz'*(not i%3) + 'Buzz'*(not i%5) or str(i) for i in range(1, 101)]) 

This example is based on what happens when string multiplying by python:

 >>> "Noelkd" * False '' 

And that does not evaluate True 0:

 >>> not True == 0 True 

Used for this fall into two categories:

  • It is more difficult to read the code.

  • Competing in golf code competitions.

+2
source

Source: https://habr.com/ru/post/1500390/


All Articles