Relation and Logic are the fundamental bricks of a program that defines its functionality. With these fundamentals, you decide what should be the flow of execution and what conditions should be kept to make sure the flow stays that way.
In every programming language including python, to manage the flow of any program, conditions are required, and to define those conditions, relational and logical operators are required.
Remember those days when your mathematics teacher in school used to ask you if 3 is greater than 2, say yes, otherwise no, that is pretty much what we do in programming world too.
You provide the compiler with some condition based on an expression, compiler computes the expression and executes the condition based on the output of the expression. In the case of relational and logical expressions, the answer will always be either True
or False
.
Operators are the conventional symbols, that bring one, two or more operands together to form an expression. Operators and operands are two key deciding factors of the output.
Now let's see some operators that are available in python language.
Python Relational Operator
Relational operators are used to establish some sort of relationship between the two operands. Some of the relevant
examples could be less than, greater than or equal to operators. Python language is capable of understanding these types of operators and accordingly return the output, which can be either True
or False
.
Let's checkout a few relational expressions. Open your IDLE and try this:
>>> 5 < 9
True
Since 5
is less than 9
, thus the output returned is True
.
The list of operators available includes:
- Less than → used
with
- Equal to → used with
==
- Not equal to → used with
!=
- Less than or equal to → used with
=
You can try each of the operators to practice with some numbers [or even strings].
>>> "abc" > "aaa"
>>> "abc" == "bcd"
True False
Python Logical Operators
Logical operators, as the name suggests are used in logical
expressions where the operands are either True
or False
. The operands in a logical expression, can be expressions which returns True
or False
upon evaluation. There are three basic types of logical operators:
- Logical AND: For AND operation the result is
True
if and only if both operands areTrue
. The keyword used for this operator isand
. - Logical OR: For OR operation the result is
True
if either of the operands isTrue
. The keyword used for this operator isor
. - Logical NOT: The result is
True
if the operand isFalse
. The keyword used for this operator isnot
.
Let's see a few examples:
>>> True and False
False
>>> not True
False
Now, we also know that the relational expressions return a Boolean value as their output, therfore know we can combine relational and logical expressions to create something more meaningful. For example,
>>> [2 < 3] and [2 < 5]
True
>>> [2 < 3] and [2 < 1]
False
Taking a bit more realistic programming example, consider you have a variable x
as input and you want to check if the user entered value is between some range, say 0 to 100, then:
>>> x = int[input[]]
25
>>> [x > 0] or [x < 100]
True
Given numbers a, b, c, and d. The following table lists examples combining relational and logical operators.
3 Precedence Rules
Motivation: Consider the following expression:
a > b && b == c || c < d
There are two interpretations which give two different results
[a > b && b > c] || c < d //do first two relational operators first, then the last, value is true
a > b && [b >c || c < d] // do last two relational operators first, then the first, value is false
Precedence rules let the compiler decide which interpretation to take. The precedence rules are:
do relational operators first,
then equality,
then the logical AND [&&]
then the logical OR [||].
By these rules, the first interpretation is used.
However, if in doubt, use parenthesis to make your meaning clear to yourself and other programmers.
4 Tips
Do not confuse the logical operator “==” [ double equal signs] with the assignment operator “=” [single equals sign]. It can lead to logical errors that the compiler will not catch. [click for experiment about this]
Use pararenthesis even if not necessary to make your logical expressions easier to read.
When trying to decide whether you need a simple comparison [‘’] or a compound one [ “=”] mentally run the program for a simple case [e.g. like executing the loop 0 or 1 time]. Off by one errors are common in logical expressions.
Always mentally check the boundary conditions.
5 Experiment
Difference between the assignment and equality
#include
using namespace std;
int main[]
{
int i = 4;
int j = 5;
if [ i = j] {
cout