# What are Logical Operators in Python?

Video Tutorial
FREE
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

In Python, logical operators carry out logical operations and return Boolean values based on the result. We need to verify multiple conditions simultaneously or use logical operators by combining multiple conditions in some cases. There are three types of logical operators in Python:

1. And Operator
2. Or Operator
3. Not Operator

Let's discuss all of these logical operators one by one in detail.

## And Operator

The And operator is used to verify where both conditions associated with it are True Simultaneously. It is represented as "X and Y".

The conditions that can occur are:

• If Both are True, then the result is True.
• If one is True and the other is False, the result is False.
• If both are False, the result is False.

Sample Example:

### Explanation:

The above example verifies whether both conditions are true or not. In the first case, they are true as 7 > 5 and 7 < 10, so it returns true, but in the second case, 7 < 5 is false, so it returns false.

Note: And Operator returns True if both conditions are true; else, it returns False.

:::

## Or Operator

The Or Operator is used to verify that either of the associated conditions is true. It is represented as "X or Y".

The conditions that can occur are:

• If Both are True, the result is True
• If One is True and the other is False, the result is True
• If both are False, the result is False

Sample Example:

### Explanation:

In the above example, in the first case, the first condition, i.e., 10  < 5, is false, whereas 10 < 15 is true, so the result is true, but in the second case, both the conditions are false, i.e., 10 < 7 and 10 <  5, so the answer is false.

Note: Alternatively, the OR operator can return true. If either of the conditions is true, it returns True; otherwise, it returns False.

## Not Operator

The Not Operator is associated with a single condition. It inverts the results, i.e., true is changed to false, and false is changed to true. It is represented as "not X".

The conditions that occur are:

• If the condition is True, the result is False
• If the condition is False, the result is True

Sample Example:

### Explanation:

In the above example, it is visible that the Not Logical Operator changes true to False and Changed False to true in Second Case.

Note: The Not Operator inverts the result. It changes True to False and vice versa.

## Order of Evaluation of Logical Operators

As there are three logical operators, we need to evaluate any expression to evaluate these logical operators. The precedence of logical operators in Python is:

The order of evaluation of logical operators in Python is as follows:

• Not operator is at the highest precedence.
• And operator is at medium precedence.
• Or operator has the least precedence.

Sample Example:

• X or y and z: This will be converted into (x or (y and z)) as the OR operator has lower precedence than the AND operator.
• not x and y or z: This will be converted into (((not x) and y) or z), as NOT has the highest precedence so that it will be evaluated first and then AND operator and at last OR operator because of the least precedence.