In many programming situations, checking a single condition is not enough. A program often needs to evaluate multiple conditions before making a decision. For example, a bank may approve a loan only if the applicant has a good credit score and a stable income. An online shopping website may offer free shipping if the purchase amount is above a certain value or the customer has a premium membership. Similarly, a login system may deny access if the entered password is not correct. These situations are handled using logical operators.
Logical operators allow you to combine multiple conditions into a single expression. Instead of writing several separate conditions, you can use logical operators to create more efficient and readable programs. Python provides three logical operators: and, or, and not.
Logical operators always work with Boolean values. Since comparison operators return either True or False, logical operators are commonly used together with comparison operators to build complex decision-making statements.
Whether you are developing websites, desktop applications, automation scripts, business software, or Machine Learning solutions, logical operators play a vital role in implementing business rules and validating conditions.
In Data Analytics, logical operators are frequently used for filtering datasets, selecting records that satisfy multiple conditions, validating data quality, identifying trends, and creating business reports. Analysts often combine multiple filters to extract meaningful insights from large datasets.
In this lesson, you will learn how Python logical operators work, understand Boolean logic, and explore practical coding examples, business scenarios, and Data Analytics applications.
After completing this lesson, you will be able to:
and, or, and not operators.Logical operators are operators that combine one or more Boolean expressions and return a Boolean result. They help determine whether one or multiple conditions are satisfied.
Logical operators are usually used with comparison operators because comparison operators produce Boolean values.
For example:
age = 20
print(age >= 18)
Output:
True
Now suppose you also want to check whether the person has a valid identity card. Instead of checking the conditions separately, you can combine them using a logical operator.
age = 20
has_id = True
print(age >= 18 and has_id)
Output:
True
Since both conditions are true, the entire expression evaluates to True.
Many real-world problems require evaluating multiple conditions simultaneously. Logical operators make these evaluations simple, readable, and efficient.
They are commonly used for:
Python provides three logical operators.
| Operator | Description | Example | Result |
|---|---|---|---|
| and | Returns True if all conditions are true. |
10 > 5 and 20 > 10 | True |
| or | Returns True if at least one condition is true. |
10 > 20 or 15 > 10 | True |
| not | Reverses the Boolean value. | not (10 > 5) | False |
Before learning logical operators, it is important to understand Boolean values. Python has two Boolean constants:
TrueFalseLogical operators always evaluate Boolean expressions and return either True or False.
print(True)
print(False)
Output:
True
False
print(15 > 10)
print(5 == 8)
Output:
True
False
The comparison operators produce Boolean values, which can then be combined using logical operators.
The and operator returns True only if all conditions are true. If even one condition is false, the result becomes False.
condition1 and condition2
print(18 > 10 and 25 > 20)
Output:
True
print(18 > 10 and 15 > 25)
Output:
False
Since the second condition is false, the overall result is False.
age = 22
salary = 55000
print(age >= 18 and salary >= 50000)
Output:
True
Both conditions are satisfied, so Python returns True.
A customer receives a premium membership only if they are at least 18 years old and have completed identity verification.
age = 24
verified = True
print(age >= 18 and verified)
Output:
True
Suppose an analyst wants to identify products that have both high sales and high customer ratings.
sales = 18000
rating = 4.7
print(sales > 15000 and rating >= 4.5)
Output:
True
This condition returns True only when both sales and rating satisfy the specified criteria.
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
In this section, you learned the fundamentals of Python logical operators and understood how they work with Boolean values. You explored the purpose of logical operators, learned about the Boolean constants True and False, and studied the and operator with syntax, truth table, practical programming examples, business scenarios, and Data Analytics applications. In the next section, you will learn the or and not operators, truth tables, and how to combine multiple logical conditions effectively.
The or operator returns True if at least one of the conditions is true. It returns False only when all conditions are false.
This operator is useful when multiple alternative conditions can satisfy a requirement.
condition1 or condition2
print(15 > 10 or 5 > 20)
Output:
True
Since the first condition is true, the entire expression becomes True.
print(8 > 20 or 30 > 25)
Output:
True
print(5 > 10 or 15 < 12)
Output:
False
A customer receives free shipping if the order value is at least $100 or the customer has a premium membership.
order_amount = 80
premium_member = True
print(order_amount >= 100 or premium_member)
Output:
True
Although the order amount is less than $100, the customer is a premium member, so the condition evaluates to True.
An analyst wants to identify products that have either high sales or excellent customer ratings.
sales = 12000
rating = 4.8
print(sales > 15000 or rating >= 4.5)
Output:
True
The rating satisfies the condition, so the overall result is True.
| Condition 1 | Condition 2 | Result |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
The not operator reverses a Boolean value. If a condition is True, the not operator returns False. If a condition is False, it returns True.
not condition
print(not True)
Output:
False
print(not False)
Output:
True
age = 15
print(not (age >= 18))
Output:
True
The expression age >= 18 is False. The not operator reverses it to True.
A website blocks users whose accounts are not active.
account_active = False
print(not account_active)
Output:
True
The result indicates that the account is inactive.
An analyst wants to identify records that are not marked as verified.
verified = False
print(not verified)
Output:
True
| Condition | Result |
|---|---|
| True | False |
| False | True |
Python allows you to combine multiple logical operators in a single expression. This makes it possible to evaluate complex conditions efficiently.
age = 19
marks = 82
print(age >= 18 and marks >= 60)
Output:
True
marks = 92
sports_quota = False
print(marks >= 90 or sports_quota)
Output:
True
logged_in = True
print(not logged_in)
Output:
False
experience = 6
salary = 70000
verified = True
print(experience >= 5 and salary >= 50000 and verified)
Output:
True
This example combines three conditions using the and operator.
| Situation | Operator |
|---|---|
| All conditions must be true. | and |
| At least one condition must be true. | or |
| Reverse a Boolean value. | not |
In this section, you learned how the or and not logical operators work in Python. You explored their syntax, truth tables, practical programming examples, business scenarios, and Data Analytics applications. You also learned how multiple logical operators can be combined to evaluate complex conditions. In the next section, you will explore real-world business examples, Data Analytics use cases, common mistakes, and best practices for writing effective logical expressions.
Logical operators are widely used in real-world applications because many business rules depend on multiple conditions. They help programs make intelligent decisions by evaluating one or more Boolean expressions.
A student is eligible for admission only if they are at least 18 years old and have scored at least 60% marks.
age = 19
marks = 72
print(age >= 18 and marks >= 60)
Output:
True
A bank approves a loan only if the applicant has a monthly income of at least $4,000 and a credit score of at least 700.
income = 5200
credit_score = 735
print(income >= 4000 and credit_score >= 700)
Output:
True
An online shopping website offers free shipping if the customer spends at least $100 or has a premium membership.
purchase = 80
premium_member = True
print(purchase >= 100 or premium_member)
Output:
True
A user can access a dashboard only if they are logged in and their account is active.
logged_in = True
account_active = True
print(logged_in and account_active)
Output:
True
A student passes the examination if they score at least 40 marks and have attendance of at least 75%.
marks = 58
attendance = 82
print(marks >= 40 and attendance >= 75)
Output:
True
Logical operators are extensively used in Data Analytics for filtering data, validating records, and generating reports. Analysts often combine multiple conditions to extract meaningful information from datasets.
An analyst wants to identify customers whose purchase amount is greater than $5,000 and whose membership status is Premium.
purchase = 6200
premium = True
print(purchase > 5000 and premium)
Output:
True
A company considers a salesperson a top performer if monthly sales exceed $25,000 or customer satisfaction is at least 4.8.
sales = 22000
rating = 4.9
print(sales > 25000 or rating >= 4.8)
Output:
True
Suppose an analyst wants to identify records that are not marked as invalid.
invalid_record = False
print(not invalid_record)
Output:
True
An HR analyst wants employees who have at least five years of experience and a performance rating above 4.5.
experience = 7
rating = 4.7
print(experience >= 5 and rating > 4.5)
Output:
True
A retail analyst wants products that either have stock greater than 100 units or are marked as featured products.
stock = 80
featured = True
print(stock > 100 or featured)
Output:
True
= instead of == inside logical expressions.and when or should be used.not operator on Boolean values.Predict the output before running each program.
print(True and False)
print(True or False)
print(not True)
age = 20
marks = 75
print(age >= 18 and marks >= 60)
salary = 3500
experience = 6
print(salary >= 4000 or experience >= 5)
In this section, you explored practical applications of Python logical operators through real-world business scenarios and Data Analytics examples. You learned how logical operators help implement business rules, validate data, filter records, and make decisions based on multiple conditions. You also reviewed common beginner mistakes and best practices for writing clear and efficient logical expressions. In the final section, you will complete the lesson with a summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Identity Operators.
In this lesson, you learned how Python logical operators help combine and evaluate multiple conditions. Logical operators always return a Boolean value (True or False) and are fundamental for building decision-making logic in Python programs.
You explored the three logical operators provided by Python: and, or, and not. You learned that the and operator returns True only when all conditions are true, the or operator returns True if at least one condition is true, and the not operator reverses the Boolean value of an expression.
Through practical coding examples, business scenarios, and Data Analytics applications, you saw how logical operators are used in authentication systems, eligibility checks, filtering datasets, validating records, and implementing business rules. Mastering logical operators is essential because they are used extensively with conditional statements such as if, elif, else, loops, and data processing tasks.
and, or, and not.and operator returns True only if all conditions are true.or operator returns True if at least one condition is true.not operator reverses the Boolean value of a condition.Logical operators combine one or more Boolean expressions and return either True or False.
Python provides three logical operators: and, or, and not.
and operator return True?It returns True only when all conditions evaluate to True.
or operator return False?It returns False only when all conditions are False.
not operator do?It reverses the Boolean value of a condition. If the condition is True, it becomes False, and vice versa.
Yes. Multiple logical operators can be combined to evaluate complex conditions.
Comparison operators produce Boolean values, and logical operators combine those Boolean results to evaluate multiple conditions.
Yes. The keywords and, or, and not must be written in lowercase.
They are commonly used in authentication systems, business rules, eligibility checks, filtering data, and conditional statements.
They help analysts filter datasets, validate records, combine multiple conditions, and generate meaningful reports.
not operator to check whether an account is inactive.and operator.or operator.and, or, and not.and operator return True?or operator return False?not operator work?and, or, and not.Create a Python program that determines whether an employee is eligible for a performance bonus.
Your program should:
This mini project demonstrates how multiple logical operators can be combined to solve a practical business problem.
Congratulations! You have successfully completed the lesson on Python Logical Operators. You now understand how to combine multiple conditions using and, or, and not, making your Python programs capable of handling more advanced decision-making.
In the next lesson, you will learn Python Identity Operators. You will explore the is and is not operators, understand the difference between identity and equality, and learn how Python compares object identities in memory with practical examples and Data Analytics use cases.