Logical Operators: and, or, not
Introduction to Logical Operators
Logical operators are used to evaluate multiple conditions at once. They help in combining or modifying boolean conditions. Python’s logical operators include and, or, and not.
The and Operator
The and operator returns True if and only if all the conditions separated by and are True. Otherwise, it returns False.
Syntax:
condition1 and condition2
Examples:
Basic Example
age = 25 citizen = True # Check if the person is an adult and a citizen is_eligible = age >= 18 and citizen print(is_eligible) # Output: True
Explanation:
- The condition age >= 18 is True.
- The condition citizen is also True.
- Therefore, is_eligible is True because both conditions are true.
Example with False Conditions
age = 16 citizen = True # Check if the person is an adult and a citizen is_eligible = age >= 18 and citizen print(is_eligible) # Output: False
Explanation:
- The condition age >= 18 is False.
- Regardless of the value of citizen, is_eligible will be False because both conditions need to be true for and to return True.
The or Operator
The or operator returns True if at least one of the conditions separated by or is True. It only returns False if all conditions are False.
Syntax:
condition1 or condition2
Examples:
Basic Example
age = 25 citizen = False # Check if the person is either an adult or a citizen is_eligible = age >= 18 or citizen print(is_eligible) # Output: True
Explanation:
- The condition age >= 18 is True.
- The condition citizen is False.
- Since at least one condition is True, is_eligible is True.
Example with All False Conditions
age = 16 citizen = False # Check if the person is either an adult or a citizen is_eligible = age >= 18 or citizen print(is_eligible) # Output: False
Explanation:
- The condition age >= 18 is False.
- The condition citizen is also False.
- Since all conditions are False, is_eligible will be False.
The not Operator
The not operator inverts the value of a condition. If the condition is True, not makes it False, and if the condition is False, not makes it True.
Syntax:
not condition
Examples:
Basic Example
age = 25 # Check if the person is not a minor is_adult = not (age < 18) print(is_adult) # Output: True
Explanation:
- The condition age < 18 is False.
- The not operator inverts this value, so is_adult is True.
Example with False Condition
age = 16 # Check if the person is not an adult is_adult = not (age >= 18) print(is_adult) # Output: True
Explanation:
- The condition age >= 18 is False.
- The not operator inverts this value, so is_adult is True.
Combining Logical Operators
Logical operators can be combined to form more complex conditions.
Example:
temperature = 22 is_sunny = True is_windy = False # Check if it's a good day to go to the beach go_to_beach = (temperature > 20 and temperature < 30) and (is_sunny and not is_windy) print(go_to_beach) # Output: True
Explanation:
- The condition temperature > 20 and temperature < 30 is True.
- The condition is_sunny and not is_windy is also True.
- Both groups of conditions are true, so go_to_beach is True.
Best Practices and Common Pitfalls
- Operator Precedence: Logical operators have precedence, with not having higher precedence than and, and and having higher precedence than or. Use parentheses to clarify the intended order of operations and avoid mistakes.
- Use Parentheses for Clarity: Although Python evaluates logical operators according to their precedence, it is often helpful to use parentheses to make your intentions clear.
- Avoid Overly Complex Conditions: If your conditions become too complex, it might be better to break them down into multiple steps or simplify them to improve readability.
Advanced Example
Here’s a more complex example using all logical operators:
age = 25 citizen = False has_job = True # Check if the person is an adult and either a citizen or has a job is_eligible = age >= 18 and (citizen or has_job) print(is_eligible) # Output: True
Explanation:
- The condition age >= 18 is True.
- The condition (citizen or has_job) evaluates to True because has_job is True, even though citizen is False.
- Both main conditions are True, so is_eligible is True.