Nearby lessons
32 of 159Python - Logical Operators
- Define logical operators and how they combine conditions
- Use the and, or, and not operators
- Read and apply truth tables for logical operators
- Explain how Python treats numbers as truthy or falsy
- Understand short-circuit evaluation
Introduction
Logical operators are used to combine multiple conditions.
The result of logical operators is generally a Boolean value:
TrueFalse
Logical Operators List
| Operator | Meaning |
|---|---|
and |
Logical AND |
or |
Logical OR |
not |
Logical NOT |
1. AND Operator
The and operator returns True only when both conditions are True.
If any one condition is False, the result becomes False.
Truth Table - AND Operator
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
Example - AND Operator
Example - AND with Relational Operators
Explanation
The first condition a < b is True.
The second condition b > 10 is also True.
Since both conditions are True, the final result is True.
Example - AND (False Case)
Explanation
The first condition is True.
The second condition 20 < 10 is False.
Since one condition is False, the final result is False.
2. OR Operator
The or operator returns True if at least one condition is True.
Truth Table - OR Operator
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
Example - OR Operator
Example - OR with Relational Operators
Explanation
The first condition a > b is False.
The second condition b > 10 is True.
Since one condition is True, the final result is True.
Example - OR (False Case)
Explanation
Both conditions are False.
Therefore, the final result is False.
3. NOT Operator
The not operator reverses a Boolean value.
Truth Table - NOT Operator
| Input | Result |
|---|---|
| True | False |
| False | True |
Example - NOT Operator
Example - NOT with Condition
Explanation
The condition a > 5 is True.
The not operator reverses it to False.
Logical Operators with Numbers
In Python:
0is treated asFalse.- Any non-zero value is treated as
True.
Example - AND with Numbers
Example - OR with Numbers
Important Rule
AND Operator:
- Returns the first
Falsevalue. - If all values are
True, it returns the last value.
OR Operator:
- Returns the first
Truevalue. - If all values are
False, it returns the last value.
Short Circuit Evaluation
Python supports Short Circuit Evaluation.
If the final result can be determined from the first condition, Python does not evaluate the remaining conditions.
Example - Short Circuit with AND
Why No Error?
The first value is False.
Python already knows the final result will be False, so it skips evaluating 10 / 0.
Example - Short Circuit with OR
Why No Error?
The first value is True.
Python skips the second expression because the final result is already known.
Using Logical Operators in if Statement
Using Logical Operators in Login Validation
Difference Between AND and OR
| AND | OR |
|---|---|
Both conditions must be True. |
At least one condition must be True. |
Real World Usage
Logical operators are commonly used in:
- Login systems
- Decision making
- Input validation
- Searching
- Filtering data
- Logical operators combine multiple conditions
- and returns True only when both conditions are True
- or returns True when at least one condition is True
- not reverses a Boolean value
- 0 is treated as False and any non-zero value as True
- Short-circuit evaluation skips unnecessary conditions