5. Logical Operators

  • The logical operators are used to combine or manipulate  Boolean values to provide a specific result to be either true or false. Python supports three logical operators namely and, or, not.  These operators are used to make decisions based on multiple conditions or to negate a condition.

Operator

Usage

Description

Example

and

a and b

‘and’ operator returns True if only when both the operands are true. Otherwise returns False

If a=10, b=15

(a>5 and b<10) – True

(a>10 and b<10) – False

or

a or b

‘or’ operator returns True if at least one operand is true. If both operators are false, then it returns False.

If a=10, b=15

(a>12 or b<10) – True

(a>10 or b<10) – False

not

a not b

‘not’ operand negates the value of it’s operand. It returns True if the operand is False and it returns False if the operand is True.

If a=10, b=15

not a>b – True

not a<b - False


The following video resource provides you the information about logical operators and examples which will help you for better understanding of Logical operators in Python.

"2.7 - Logical Operators" by , CodeGeek Learning is licensed under CC BY-SA 3.0