Introduction to Boolean Operators in Python
The operators such as not, and, or that are used to perform logical operations in Python, with results of the operations involving them being returned in TRUE or FALSE. The not operator having the highest priority, followed by the and operator also the or operator being the lowest in the order of the priority, and that the not operator has lower priority than non-Boolean operators. in Python programming language the and as well as or operator known as the short-circuit operators, are also called as Boolean operators
The datatypes like Integer, Float, Double, String, etc. have the possibility to hold unlimited values, variables of type Boolean can have one of the two values which are either TRUE or FALSE. In Python as a programming language, True and False values are represented as a string without enclosing them in double or single inverted commas and they always start with the uppercase T and F. Let’s consider an example to understand more –
>>> bool_var = True
In the above example, the variable named bool_var stores the Boolean value of True and when you print it out on the terminal, it shows True as the value.
By default, the Boolean value True is True in Python and False is False in Python.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
This above example shows that the string spelled as true with a lowercase T is treated as a variable and not as a Boolean value.
>>> True = 3+5
File "<stdin>", line 1
SyntaxError: can't assign to keyword
This example shows that we cannot assign any values or expressions to the Boolean Values True or False in Python.
>>> a = 1
>>> a = 0
>>> a = “some string”
>>> a = “”
From the above example, it can be seen that any value for a numeric datatype but 0 and any value for a string datatype but an empty string when typecasted to Boolean gives True value otherwise, it treats it as False.
Now that we have understood the Boolean values and their behavior in Python programming language, let us understand the Boolean Operators which is actually the main focus of this article.
Boolean Operators in Python
Boolean Operators are the operators that operate on the Boolean values and if it is applied on a non-Boolean value then the value is first typecasted and then operated upon. These might also be regarded as the logical operators and the final result of the Boolean operation is a Boolean value, True or False.
There are six comparison operators as described in the table below which evaluate the expression to a Boolean value.
Now, let us consider an example each and see how they behave in Python Programming Language.
>>> a = 1
>>> a == 1
>>> a != 10
>>> a != 1
>>> a > 10
>>> a < 12
>>> a >= 1
>>> a <= 7
So, you can see that with the integer value of 1 assigned to the variable ‘a’ and compared it with many other integral values, we get different Boolean results depending on the scenario. The value of ‘a’ can also be compared with other variables in a similar fashion.
Binary Boolean Operators
These operators are the ones that operate on two values which are both Boolean. The ‘and’ operator and the ‘or’ operator are the two binary Boolean operators that operate on some logic to give the Boolean value again. The standard Truth table for these two logical binary Boolean operators is as follows.
The truth table for the ‘and’ operator. Even if one value is false then the whole expression is False.
The truth table for the ‘or operator. Even if one value is true then the whole expression is True.
Now, let’s see some examples in Python. In Python, these operators are used by the keywords ‘and’ and ‘or’ for the ‘and’ logic and the ‘or’ logic respectively.
>>> a = True
>>> b = False
>>> a and b
>>> a or b
The ‘not’ operator is the logical Boolean Operator which compliments the current Boolean value of the variable. That is, if the value is ‘true’ then the not operator will modify it to ‘false’ and vice versa. In Python, it is represented by the keyword ‘not’.
Let’s see the ‘not’ operator in action in Python.
>>> a = True
>>> not a
>>> not not not not a
So, this is the way the ‘not’ operator works in Python.
Combination of Binary Boolean and Comparison Operators
Since the comparison operators evaluate to Boolean values and binary operators operate upon two Boolean values, we can have an expression that uses a combination of binary Boolean and comparison operators to get a Boolean resultant again.
Let’s consider a few examples and see how to exploit the feature.
>>> (5 > 3) and (7 == 7)
The first bracket evaluates True and second to True as well and the final expression will be True and True which is True.
We can also use the ‘not’ operator in this kind of expression. For example,
>>> (7 > 3) and (9 != 8) and not False
In this example too, the final ‘not False’ evaluates to True, (9 != 8) evaluates to True and (7 > 3) also evaluates to True which gives us the final expression of (True and True and True) which results to be True.
Note – The expressions inside the brackets are evaluated on priority in Python. The priority of other operators goes like this. If the expression is filled with mathematical operators, the ‘and’ operators, the ‘or’ operators and the ‘not’ operators, then the mathematical operators are evaluated first followed by the ‘not’ operators, followed by the ‘and’ operators and at the end the ‘or’ operators.
Boolean operators are one of the predominant logic that comes in handy while programming; especially while doing some decision making in the logic. Having a thorough knowledge of how they behave would make you an outstanding programmer. Happy coding!
This is a guide to Boolean Operators in Python. Here we discuss the boolean value and different boolean Operators in Python. You may also look at the following articles to learn more –