After completing this chapter, you will be able to:
- Understand Logical Operators.
- Explain why logical operators are used.
- Learn different logical operators.
- Evaluate multiple conditions.
- Understand truth tables.
- Write programs using logical operators.
Logical Operators are used to combine two or more conditions.
These operators return either True or False depending on the result of the logical expression.
Logical operators are mainly used in:
- Decision Making
- Login Systems
- Eligibility Checking
- User Authentication
- Banking Applications
Without logical operators, checking multiple conditions would be difficult.
A Logical Operator is an operator used to combine or modify Boolean expressions.
Python provides three logical operators:
andornot
Logical operators help to:
- Combine multiple conditions.
- Make complex decisions.
- Validate user input.
- Improve program logic.
- Control program execution.
| Operator | Description |
|---|---|
| and | Returns True if both conditions are True |
| or | Returns True if at least one condition is True |
| not | Reverses the Boolean value |
The and operator returns True only when both conditions are True.
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
age = 22
citizen = True
print(age >= 18 and citizen)True
The or operator returns True if at least one condition is True.
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
marks = 80
sports_quota = False
print(marks >= 75 or sports_quota)True
The not operator reverses the Boolean result.
| Condition | Result |
|---|---|
| True | False |
| False | True |
is_logged_in = False
print(not is_logged_in)True
Write a Python program to check whether a person is eligible to vote.
Conditions:
- Age should be at least 18.
- Person should be a citizen.
age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible to Vote")Eligible to Vote
A student is eligible if:
- Marks are at least 75 OR
- The student belongs to the sports quota.
marks = 60
sports_quota = True
if marks >= 75 or sports_quota:
print("Scholarship Approved")Scholarship Approved
logged_in = False
if not logged_in:
print("Please Login")Please Login
Logical operators are used in:
- Login Authentication
- ATM Applications
- Banking Systems
- Student Management Systems
- Hospital Software
- Online Shopping Websites
- Employee Management Systems
- Combines multiple conditions.
- Simplifies decision making.
- Improves code readability.
- Reduces nested if statements.
| Operator | Meaning |
|---|---|
| and | Both conditions must be True |
| or | At least one condition must be True |
| not | Reverses the Boolean value |
Example
marks = 80
sports = False
print(marks >= 75 and sports)Output
False
Reason:
Both conditions are not True.
status = True
print(not status)Output
False
Logical operators combine or modify Boolean expressions and return either True or False.
Three:
andornot
Only when both conditions are True.
When at least one condition is True.
It reverses the Boolean value.
and→ Both conditions must be True.or→ At least one condition must be True.not→ Reverses the Boolean result.- Logical operators return
TrueorFalse. - Used in decision-making and conditional statements.
In this chapter, you learned:
- What Logical Operators are.
- Types of Logical Operators.
- Truth tables.
- Practical programs.
- Real-world applications.
- Common mistakes.
- Interview questions.