PYTHON BOOLEAN

Python Boolean

The Python Boolean type is the type of python that has python’s built-in data types. It represents the correct value of an expression. Let us take an example, the expression 1<=2 is True, meanwhile, the expression 0==1 is False. To understand the behavior of values in Python Boolean is very important to do perfect programming in python.

Python Boolean Type

The python boolean has two possible values: one is True and the other is False. None of the other values will have bool as its type. One can easily check the type of True and False with the built-in type().

>>>
>>> type(False)
<class 'bool'>
>>> type(True)
<class 'bool'>

The type() of both False and True is bool.

The type bool is built in Python. It means that it is always available in Python and does not need to be imported. Moreover, the name itself is not a keyword in the language. Here is an example:

def myFunction()
return True;

print (myFunction())

Output

True

Although it is technically possible to avoid confusion. Therefore, it is highly recommended that you should not assign a different value to bool

Python Boolean Not

The not keyword will return the logical negation of a bool value. We can use the not keyword by placing it in front of any boolean expression. If an expression evaluates to True, placing not in front of it will return False.

Python Boolean Operators

In the Python programming language, we have three Boolean operators or logical operators: and, or, and not.

andTrue if both values are true
orTrue if one value is true
nottrue is the other value is false

Python Booleans as the Keywords

The built-in names in python are not keywords. Keywords in python language are regular variables. If you assign them the keyword, then you might override the built-in value. In contrast, the names True and False are not built-ins. They are the keywords. Although many other Python keywords, True and False are Python expressions. But, these expressions can be used wherever other expressions are being used, for example, 1+1 can be used as an expression.  So it is possible to assign a Boolean value to the variable, but it is quite difficult to assign a value to True.

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

This is because True is a keyword and we cannot assign a value to it. Similarly, the rule that applies to False is the same.

>>>
>>> False = 8
  File "<stdin>", line 1
SyntaxError: cannot assign to False

It is impossible to assign to False because it is a keyword in Python. Like this, True and False behave like other numeric constants. For example,, one can pass 1.7 to functions or assign it to the variables. However, it is not possible to assign a value to 1.7.  The statement 1.7=7 is not a valid Python. As both 1.7=7 and False=7 are invalid Python code and will raise a Syntax Error when parsed.

Python Booleans as Numbers

We can also call booleans as the numeric type in the python language. It means that they are the numbers for all intents and purposes. Or we can say that arithmetic operations can be applied to the Booleans, also we can compare them to the numbers.

>>>
>>> True == 1
True
>>> False == 0
True
>>> True + (False / True)
1.0

It is a case that there are not many uses for the numerical nature of the Boolean values, but there is one technique that can be useful. Because True is equal to 1 and False is equal to 0, when adding Booleans together is a quick way to count the number of True values. This helps when you need to count the number of items that satisfy a condition.