In Python, logical operators are used to combine Boolean values (True or False). The operators enable the evaluation of two or more relational operators which we covered in our previous article. There are three main logical operators, which are, "and", "or", and "not". We are going to cover these in detail as well as the "nand" and "nor" operators.
By the way, an operand is data to be manipulated or operated on. We shall use this term a lot in this article.
NB: "and", "or", "nand" and "nor" are binary operators thus they only operate on two operands. The "not" operator is unitary and thus only works on one operand. Parenthesis has to be used where more than one operand is to be evaluated so that only they can be evaluated in the desired order otherwise the default order of operands is used.
We shall use the below variables throughout our code.
1 x = 5
2 y = 10
3 z = 15
The "and" operator returns True if both operands are True; otherwise, it returns False. In other words, it returns False if either operand is False; otherwise, returns True. It evaluates the second operand only if the first operand is True. See the example below.
1 print(x < y and y < z) # True
2 print(x > y and y < z) # False
The "or" operator returns True if at least one of the operands is True; otherwise, it returns False. In other words, the operator returns False if and only if both operands are False. It evaluates the second operand only if the first operand is False. See the example below.
1 print(x < y or y > z) # True
2 print(x > y or y > z) # False
3 print(x > y or y < z) # True
The "not" operator is a unary operator that negates the value of its operand. It takes only one operand and returns True if the operand is False and False if the operand is True. See the example below.
1 print(not x > y) # True
2 print(not x < y) # False
In the above example, in line 1, x > y is False, so not x > y is True. In line 2, x < y is True, so not x < y is False.
There is no built-in "nand" operator in Python. The behaviour is defined by using a "not" operator on an "and" operator's output. The "nand" operator's behaviour is the opposite of the "and" operator. It returns True if one of the operands is False; otherwise returns True. In other words, it returns False if both operands are True. In the below example, we have to put the "and" operator in parenthesis to force an order of execution so that the "and" operation is implemented first with the result passed to the "not" operator.
1 print(not(x < y and y < z)) # False
2 print(not(x > y and y < z)) # True
There is no built-in "nor" operator in Python. The behaviour is defined by using a "not" operator on a "nor" operator output. The "nor" operator's behaviour is the opposite of an "or" operator. The "nor" operator returns True if both operands are False; otherwise, it returns False. In other words, the operator returns True if and only if both operands are False. If at least one True operand is True, the result is False.
In the below example, we have to put the "or" operation as a parenthesis to force an order of execution so that the "or" operation is implemented first with the result passed to the "not" operator.
1 print(not(x < y or y > z)) # False
2 print(not(x > y or y > z)) # True
3 print(not(x > y or y < z)) # False
Product of
Jeremiah Taguta T/A Charis Technologies