Python logical operators

Jeremiah Taguta (BSc in Computer Science, Masters in Information Systems)
Published on 15-08-2023
Demystifying Logical Operators: Exploring 'and', 'or', 'not', 'nand', and 'nor' Operators


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
  1. "and" Operator

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
In the above example, the first print statement, line 1, both x < y and y < z are True, so the result is True. In the second print statement, line 2, x > y is False, so the second operand y < z is not evaluated, and the output is False.
  1. "or" Operator

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
In the example above, the first print statement, line 1, x < y is True, so the result is True even though y > z is False. In line 2, both x > y and y > z are False, so the result is False. In line 3, x > y if false but y < z is true so the output is True.
  1. "not" Operator

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.

  1. "nand" Operator

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
In the above example, in line 1, both x < y and y < z are True, so the output of the "and" operator is True which in turn is subjected to the "not" operator returning False resultantly. In the second print statement, line 2, x > y is False, so the second operand y < z is not evaluated, so the output of the "and" operator is False which in turn is subjected to the "not" operator returning True resultantly. These outputs depict the behaviour of a "nand" gate.
  1. "nor" Operator

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
In the example above, in line 1, x < y is True, so the "or" operator result is True even though y > z is False. The False result in turn is subjected to the "not" operator returning True resultantly. In line 2, both x > y and y > z are False, so the "or" operator result is False. The False result in turn is subjected to the "not" operator returning True resultantly.
In line 3, x > y if False but y < z is True so the "or" operator result is True. The True result in turn is subjected to the "not" operator returning False resultantly.

In conclusion, we covered the "and", "or", "not", "nand" and "nor" operators. These logical operators are frequently used to direct program flow and make decisions based on logical conditions in conditional statements, loops, and boolean expressions.
Thanks for reading. If you have any questions, feel free to ask in the comments section, I'll gladly help. Check out my programming tutorials on YouTube and don't forget to subscribe, comment, like, and share on both YouTube and on this Blog. Lets code together!!!

200 Like

Drop your comment

© Copyright 2023 Jeremiah Taguta. All Rights Reserved.

Product of ‌

Jeremiah Taguta T/A Charis Technologies