How to use boolean value in Python?

Does Python contain a boolean value? I know what you can do:

checker = 1 if checker: #dostuff 

But I'm pretty pedantic and I like to see boolean in Java. For example:

 Boolean checker; if (someDecision) { checker = true; } if(checker) { //some stuff } 

Is there such a thing as a boolean in Python? I can not find anything like this in the documentation.

+105
python boolean
Nov 17 '09 at 12:48
source share
6 answers
 checker = None # not necessary if some_decision: checker = True if checker: # some stuff 

[Edit]

For more information: http://docs.python.org/library/functions.html#bool

Your code also works, since 1 is converted to True if necessary. In fact, Python did not have a boolean type for a long time (as in old C), and some programmers still use integers instead of booleans.

+135
Nov 17 '09 at 12:51
source share

Boolean embedded capitalized: True and False .

Note that you can make checker = bool(some_decision) as a shorthand bool to return only True or False .

It is good to know that the classes defining __nonzero__ or __len__ will be True or False depending on the result of these functions, but almost every other logical result will be True (except for the None object, empty sequences and numeric zeros).

+91
Nov 17 '09 at 12:54
source share

True ... and False , obviously.

Otherwise, None evaluates to False, as does the integer 0 , as well as float 0.0 (although I would not use similar floats). In addition, empty lists [] , empty tuplets () and empty lines '' or "" are evaluated as False.

Try it yourself with the bool() function:

 bool([]) bool(['a value']) bool('') bool('A string') bool(True) # ;-) bool(False) bool(0) bool(None) bool(0.0) bool(1) 

etc..

+11
Nov 17 '09 at 13:02
source share

Boolean types are defined in the documentation:
http://docs.python.org/library/stdtypes.html#boolean-values

Quote from the doc:

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 like integers 0 and 1, respectively. The built-in bool () function can be used to cast any value to a boolean if the value can be interpreted as a truth value (see the section "Testing the truth value" above).

They are written as False and True, respectively.

So, in java code, remove the brackets, change true to true , and you will be fine :)

+5
Nov 17 '09 at 13:02
source share

Yes, there is a bool data type (which inherits from int and has only two values: True and False ).

But Python also has a boolean-able concept for every object that is used when calling the bool([x]) function.

Details: object. nonzero and boolean-value-of-objects-in-python .

+3
Nov 17 '09 at 13:10
source share

Unlike Java, where you would declare boolean flag = True , in Python you can simply declare myFlag = True

Python interprets this as a boolean variable

0
Apr 27 '17 at 16:50
source share



All Articles