Python Relational and Logical Operators

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(raw_input())

25

>>> (x > 0) or (x < 100)

True