What are Logical Operators in Python?
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:
- And Operator
- Or Operator
- 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.
:::
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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:
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.
Learn More
Learn more about operators in Python:
Conclusion
It's time to conclude our topic with the most important points we have discussed. So let's see them one by one.
- We can use logical operators to obtain results simultaneously in the case of various conditions.
- In Python, there are three types of logical operators: and, or, and not.
- And Operator returns true only when both conditions are true simultaneously.
- The Or Operator returns True if either of the conditions is met.
- Not Operator inverts the result, i.e., True changes to False and Vice versa.
- The precedence of logical operators in Python is: Not < And < Or.