Nearby lessons
37 of 108Python - Logical Operators
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
main.py
True False
Example - AND with Relational Operators
main.py
True
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)
main.py
False
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
main.py
True False
Example - OR with Relational Operators
main.py
True
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)
main.py
False
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
main.py
False True
Example - NOT with Condition
main.py
False
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
main.py
20 0
Example - OR with Numbers
main.py
10 20
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
main.py
False
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
main.py
True
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
main.py
Eligible
Using Logical Operators in Login Validation
main.py
Login Successful
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
Important Notes
- Logical operators combine multiple conditions.
- The result is generally
TrueorFalse. - Python supports short circuit evaluation.
- The
notoperator reverses a Boolean value. - When used with numbers,
0is treated asFalseand non-zero values are treated asTrue.
Quick Summary
| Operator | Purpose |
|---|---|
and |
Both conditions must be True. |
or |
At least one condition must be True. |
not |
Reverses a Boolean value. |