Are they keywords, constants, or functions? If they are constants, what are their types? It seems python has no type of boolean.
-
4`None` is of type `NoneType`. `True` and `False` are of type `bool`. You can type `type(True)` or for any variable on the REPL to figure out its type – sshashank124 May 16 '18 at 05:39
-
2“It seems python has no type of boolean.” Eh? `bool`? Anyway, they’re keywords that act as constants/literals, because Python doesn’t have that kind of constant. (In Python 2, only `None` is a keyword, and you can actually assign values to `True` and `False`.) – Ry- May 16 '18 at 05:39
-
4I dont understand why this question received so many downvotes. – Bedir Yilmaz May 16 '18 at 05:42
-
1Not that I downvoted, but it very easily google-able stuff as it's been asked/answered in many different places – sshashank124 May 16 '18 at 05:44
-
Possible duplicate of [False or None vs. None or False](https://stackoverflow.com/questions/3914667/false-or-none-vs-none-or-false) – akshat May 16 '18 at 05:52
-
Open a shell and type `bool`. It comes back as `
`. So python does have a boolean type. – tdelaney May 16 '18 at 06:08 -
This is now the top answer on google for "Is None True in Python", so I think it could use a link to a canonical answer – Fr4nc3sc0NL Jun 05 '20 at 18:27
-
3I didn't understand why this question has gotten negative feedback, I think some are quick to judge and punish. – Vivek Shukla Jun 09 '20 at 13:57
1 Answers
The Python Documentation has it all, one just has to know where to look:
A small number of constants live in the built-in namespace. They are:
False
The false value of the bool type. Assignments to False are illegal and raise a SyntaxError.
True
The true value of the bool type. Assignments to True are illegal and raise a SyntaxError.
None
The sole value of the type NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to None are illegal and raise a SyntaxError.
Also
- https://docs.python.org/3/reference/lexical_analysis.html#keywords
- https://docs.python.org/3/reference/datamodel.html#the-standard-type-hierarchy
None
This type has a single value. There is a single object with this value. This object is accessed through the built-in name None. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false.
and
Booleans (bool)
These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively.
- https://docs.python.org/3/library/functions.html#bool
- https://docs.python.org/3/library/stdtypes.html#truth
Further reading
-
1https://docs.python.org/3/library/constants.html is a good place to look! – tdelaney May 16 '18 at 06:06
-
I'm still confused. They are listed as keywords in https://docs.python.org/3/reference/lexical_analysis.html#keywords, but as constants in https://docs.python.org/3/library/constants.html. So, they are keywords and constants at the same time? – peter Nov 28 '21 at 15:52