Boolean Operators [Python]
The three Boolean operators (and, or, and not) are used to compare Boolean values. Like comparison operators, they evaluate these expressions down to a Boolean value. Let’s explore these operators in detail, starting with the and operator.
Enter some expressions using and into the interactive shell to see it in action.
A truth table shows every possible result of a Boolean operator. The truth table for the and operator.
On the other hand, the or operator evaluates an expression to True if either of the two Boolean values is True. If both are False, it evaluates to False.
You can see every possible outcome of the or operator in its truth table, shown in Table:
The not Operator
Unlike and and or, the not operator operates on only one Boolean value (or expression). The not operator simply evaluates to the opposite Boolean value.
Much like using double negatives in speech and writing, you can nest not operators u, though there’s never not no reason to do this in real programs.
The Table will shows the truth table for not.
Mixing Boolean and Comparison Operators
Since the comparison operators evaluate to Boolean values, you can use
them in expressions with the Boolean operators.
Recall that the and, or, and not operators are called Boolean operators
because they always operate on the Boolean values True and False. While
expressions like 4 < 5 aren’t Boolean values, they are expressions that evaluate
down to Boolean values. Try entering some Boolean expressions that
use comparison operators into the interactive shell.
The computer will evaluate the left expression first, and then it will evaluate the right expression. When it knows the Boolean value for each, it will then evaluate the whole expression down to one Boolean value. You can think of the computer’s evaluation process for (4 < 5) and (5 < 6) as shown in Figure below.
You can also use multiple Boolean operators in an expression, along with the comparison operators.
The Boolean operators have an order of operations just like the math operators do. After any math and comparison operators evaluate, Python evaluates the not operators first, then the and operators, and then the or operators.
Boolean Operators [Python] |
Binary Boolean Operators
The and and or operators always take two Boolean values (or expressions), so they’re considered binary operators. The and operator evaluates an expression to True if both Boolean values are True; otherwise, it evaluates to False.Enter some expressions using and into the interactive shell to see it in action.
A truth table shows every possible result of a Boolean operator. The truth table for the and operator.
The and Operator’s Truth Table |
On the other hand, the or operator evaluates an expression to True if either of the two Boolean values is True. If both are False, it evaluates to False.
>>> False or TrueTrue>>> False or FalseFalse
You can see every possible outcome of the or operator in its truth table, shown in Table:
The or Operator’s Truth Table |
Unlike and and or, the not operator operates on only one Boolean value (or expression). The not operator simply evaluates to the opposite Boolean value.
>>> not TrueFalse
>>> not not not not TrueTrue
Much like using double negatives in speech and writing, you can nest not operators u, though there’s never not no reason to do this in real programs.
The Table will shows the truth table for not.
The not Operator’s Truth Table |
Since the comparison operators evaluate to Boolean values, you can use
them in expressions with the Boolean operators.
Recall that the and, or, and not operators are called Boolean operators
because they always operate on the Boolean values True and False. While
expressions like 4 < 5 aren’t Boolean values, they are expressions that evaluate
down to Boolean values. Try entering some Boolean expressions that
use comparison operators into the interactive shell.
>>> (4 < 5) and (5 < 6)True>>> (4 < 5) and (9 < 6)False>>> (1 == 2) or (2 == 2)True
The computer will evaluate the left expression first, and then it will evaluate the right expression. When it knows the Boolean value for each, it will then evaluate the whole expression down to one Boolean value. You can think of the computer’s evaluation process for (4 < 5) and (5 < 6) as shown in Figure below.
The | process of evaluating | (4 < 5) and | (5 < 6) to True. |
You can also use multiple Boolean operators in an expression, along with the comparison operators.
>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2 True
The Boolean operators have an order of operations just like the math operators do. After any math and comparison operators evaluate, Python evaluates the not operators first, then the and operators, and then the or operators.
I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks 123moviesfree.rocks
ReplyDeleteThanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Course in Chennai
It contains more than 5,000 different drivers. It provides you the list of drivers for individual updating. Driver Updater Pro serial key provides backup Driver Toolkit Download
ReplyDelete