top of page

Python: Booleans

When you compare two values, the expression is evaluated and Python returns the Boolean answer.

The Python Boolean type has only two possible values:

True
False
No other value will have bool as its type. You can check the type of True and False with ():

type(False)
<class 'bool'>
type(True)
<class 'bool'>
The type() of both False and True is bool.

>>> bool
<class 'bool'>
>>> bool = "this is not a type"
>>> bool
'this is not a type'

Python Booleans as Keywords
Built-in names aren’t keywords. As far as the Python language is concerned, they’re regular variables. If you assign to them, then you’ll override the built-in value.
I
It’s possible to assign a Boolean value to variables, but it’s not possible to assign a value to True:

>>> a_true_alias = True
>>> a_true_alias
True
>>> True = 5
File "<stdin>", line 1
SyntaxError: cannot assign to True

Because True is a keyword, you can’t assign a value to it. The same rule applies to False:

>>> False = 5
File "<stdin>", line 1
SyntaxError: cannot assign to False
You can’t assign to False because it’s a keyword in Python.

©2020 by Global Tech Nomads. 
Special thanks to X

bottom of page