Difference between logical and relational operators in python

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:

  1. Less than → used with <
  2. Greater than → used with >
  3. Equal to → used with ==
  4. Not equal to → used with !=
  5. Less than or equal to → used with <=
  6. Greater 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:

  1. Logical AND: For AND operation the result is True if and only if both operands are True. The keyword used for this operator is and.
  2. Logical OR: For OR operation the result is True if either of the operands is True. The keyword used for this operator is or.
  3. Logical NOT: The result is True if the operand is False. The keyword used for this operator is not.

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 (‘<’ and ‘>’) or a compound one ( “<=” and “>=”) 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 << "equal" << endl;
    } else {
        cout << "NOT equal" << endl;
    }
    return 0;
}

Hypothesis: What will this program print out?

  • equal?

  • NOT equal?

If you aren’t sure, compile and run the program to see.

What is the value of “i” after the “if” statement executes? (you can modify the experiment to output the value)

How does relational operator differ from logical operators?

Relational, equality and logical operators are used to form logic expressions. Relational and equality operators usually compare two numbers and return a value of true or false (forming a logic expression). Logical operators combine logical values of true or false into a logical expression.

What are the 6 relational operators in Python?

We have six of these, including and limited to- less than, greater than, less than or equal to, greater than or equal to, equal to, and not equal to. So, let's begin with the Python Comparison operators.

What is an logical operator in Python?

In Python, Logical operators are used on conditional statements (either True or False). They perform Logical AND, Logical OR and Logical NOT operations. OPERATOR.

What is the difference between logical and and logical or operator?

The ampersand (&) symbol is a valid substitute for the logical operator AND . The vertical bar ( | ) is a valid substitute for the logical operator OR . Only one logical operator can be used to combine two relations. However, multiple relations can be combined into a complex logical expression.