Python Boolean Operators: and, or, not Explained

Python Boolean Operators: and, or, not Explained

Python Boolean Operators: and, or, not Explained

Logical operators are essential tools in Python for evaluating expressions and making decisions based on conditions. These operators—AND, OR, and NOT—help combine or modify conditions to control the flow of your programs. Whether you’re building a decision-making system, filtering data, or implementing complex logic, understanding how to use these operators effectively is key.In this guide, we’ll explore the three main logical operators in Python, how to use them, and some practical examples to demonstrate their application. We’ll also delve into their precedence and associativity rules to ensure you write efficient and accurate Python code.

What Are Logical Operators in Python?

 Logical operators in Python are used to combine or modify Boolean expressions, resulting in a final Boolean value (True or False). These operators are typically used in conditional statements to control the flow of a program.The primary logical operators in Python are:
  • AND (and)
  • OR (or)
  • NOT (not)
These operators allow you to make decisions in your code based on multiple conditions or invert the result of a condition.

Types of Logical Operators in Python

Let’s break down how each logical operator works in Python, along with some practical examples.

1. AND (and Operator)

The AND operator is used to check if both conditions are True. It returns True only when both expressions are True. If either expression is False, it returns False.Example:
python
# Using AND operator a, b = 10, 20 print((a < b) and (a != b)) # True
Explanation:
  • a < b is True, and a != b is also True. Since both conditions are True, the output is True.
Another Example:
python
# Using AND operator with one False condition a, b = 10, 20 print((a == b) and (a < b)) # False
Explanation:
  • a == b is False, so the entire expression returns False regardless of the second condition.

2. OR (or Operator)

The OR operator checks if at least one of the conditions is True. It returns True if either expression is True, otherwise, it returns False.Example:
python
# Using OR operator a, b = 10, 20 print((a > b) or (a != b)) # True
Explanation:
  • a != b is True, so the output is True, even though a > b is False.
Another Example:
python
# Using OR operator with both conditions False a, b = 10, 20 print((a - b == a + b) or a != b) # True
Explanation:
  • The left side condition is False, but the right side (a != b) is True, so the result is True.

3. NOT (not Operator)

The NOT operator reverses the Boolean value of the expression. If the expression is True, it becomes False, and vice versa.Example:
python
# Using NOT operator a, b = 10, 20 print(not(a > b or a != b)) # False
Explanation:
  • The result of (a > b or a != b) is True. Since we applied the NOT operator, it reverses the result to False.

How to Use Logical Operators in Real-World Scenarios

Logical operators are often used in real-world situations to filter data or create conditions for actions. Here are a couple of practical examples:

Example 1: Exam Eligibility Criteria

Suppose you need to check whether a student is eligible to attend an exam based on two conditions: they must have paid the exam fee and have attended at least 75% of classes.
python
# Exam eligibility check paid_exam_fee = True attended_75_percent = Trueif paid_exam_fee and attended_75_percent: print(“Eligible to attend the exam”) else: print(“Not eligible to attend the exam”)
Explanation:
  • The AND operator ensures both conditions must be True for eligibility.

Example 2: Identifying Active Customers

In an e-commerce setting, you may want to identify active customers based on two conditions: they must have made a purchase in the last 3 months or have some wallet credits.
python
# Identifying active customers made_purchase_last_3_months = False has_wallet_credits = Trueif made_purchase_last_3_months or has_wallet_credits: print(“Active customer”) else: print(“Inactive customer”)
Explanation:
  • The OR operator allows customers who meet either of the conditions to be considered active.

Precedence & Associativity of Logical Operators in Python

Logical operators in Python follow specific rules of precedence and associativity that determine the order in which expressions are evaluated.
  • Precedence: All logical operators (and, or, not) have the same precedence, but not has higher precedence over and, and and has higher precedence over or.
  • Associativity: The associativity of these operators is left to right. This means that the left-most operator is evaluated first when multiple operators appear in an expression.
Example:
python
# Evaluating logical operators a, b = 10, 20 print(not(a > b and a != b) or a != b) # True
Explanation:
  • First, the NOT operator is applied to (a > b and a != b).
  • The result is then used in the or expression.

Frequently Asked Questions About Logical Operators in Python

1) What are Logical Operators in Python?

Logical operators in Python are used to evaluate multiple conditions and return a Boolean value (True or False). The three main logical operators are:
  • and: Returns True if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Reverses the result of the condition.

2) Can Logical Operators Be Used on Strings?

Yes, logical operators can be used with strings in Python. Here’s how:
  • An empty string is considered False, while a non-empty string is considered True.
  • and returns the second operand if the first operand is True, and the first operand if it’s False.
  • or returns the first operand if it’s True, and the second operand if it’s False.
Example:
python
str1 = '' str2 = 'Python' print(str1 and str2) # Output: '' print(str1 or str2) # Output: 'Python'

Conclusion

Logical operators are a powerful tool in Python, allowing you to combine and modify conditions in your code. Whether you’re building complex decision-making systems, filtering data, or controlling program flow, mastering these operators is essential. By understanding how and, or, and not work, along with their precedence and associativity rules, you can write clean, efficient, and effective Python code.Click here to know more our program! 
c