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 has the highest priority, followed by the operator and operator being the lowest in the order of the priority. The not operator has lower priority than non-Boolean operators. In Python programming language, the and as well as or operator is known as the short-circuit operators, are also called as Boolean operators
The data types like Integer, Float, Double, String, etc., have the possibility to hold unlimited values; variables of type Boolean can have one of the two values: 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.
>>> bool_var = True >>> 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.
>>> True >>> True
By default, the Boolean value True is True in Python and False is False in Python.
>>> true Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'true' is not defined
The 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 >>> bool(a) True >>> a = 0 >>> bool(a) False >>> a = “some string” >>> bool(a) True >>> a = “” >>> bool(a) False
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.
Different 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.
1. Comparison Operators
As described in the table below, there are six comparison operators, 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 True >>> a != 10 True >>> a != 1 False >>> a > 10 False >>> a < 12 True >>> a >= 1 True >>> a <= 7 True
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.
2. 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 example 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 False >>> a or b True
3. Not Operator
The ‘not’ operator is the logical Boolean Operator, which compliments the variable’s current Boolean value. 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 False >>> not not not not a >>> True
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) True
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.
>>> (7 > 3) and (9 != 8) and not False True
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.
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.
This is a guide to Boolean Operators in Python. Here we discuss the boolean value and different boolean operators in Python in detail. You may also look at the following articles to learn more –