28
AugLogical Operators Python
Logical Operators in Python: An Overview
Python logical operators (and, or, not) are used in conditional or decision-making statements for combining multiple conditions. They return the result as true or false based on the expression.
In this Python tutorial, you'll learn how to use logical operators (and, or, not) with clear examples, truth tables, and mini projects to expand your understanding.
👉 Enroll in our Free Python Course With Certificate and boost your coding skills today!
What are the Logical Operators in Python?
Logical operators in Python are operators that are used in Python conditional statements for combining multiple conditions in a single expression. Logical operators are used to create complicated logics in our program. An if statement with and, or, and not in Python enables more dynamic and flexible decision-making.
The three logical operators available in Python are and, or, and not.
Let’s understand and, or, and not in detail.
Syntax
operand_1 logical_operator operand_2
Truth Table for Logical Operators in Python
Operand1 | Operand2 | Operand1 AND Operand2 | Operand1 OR Operand2 | NOT (Operand1) | NOT (Operand2) |
T | T | T | T | F | F |
T | F | F | T | F | T |
F | T | F | T | T | F |
F | F | F | F | T | T |
Read More - 50 Python Interview Questions
Types of Logical Operators
1. AND Operator
AND operator returns True only if both of its operands are true; otherwise, it returns False. It is often used to check multiple conditions simultaneously.
Syntax
operand_1 and operand_2
Example
x = True
y = False
result = x and y
print(result)
Explanation
Output
False
2. OR Operator
The OR operator returns True if at least one of its operands is true; otherwise, it returns False. It provides a flexible way to handle situations where multiple conditions may lead to the same outcome.
Syntax
operand_1 or operand_2
Example
a = True
b = False
result = a or b
print(result)
Explanation
Output
True
Read More - Python Developer Salary
3. NOT Operator
The not operator is a unary operator that returns the opposite Boolean value of its operand. If the operand is True, not returns False, and vice versa.
Syntax
operand_1 not operand_2
Example
z = True
result = not z
print(result)
Explanation
In this example, a Boolean variable z is assigned the value True. Subsequently, the not operator is applied to the variable z, resulting in the negation of its Boolean value.
Output
False
Examples of Logical Operators in Python Compiler
Example 1: Checking Age Eligibility
age = 25
if age >= 18 and age <= 60:
print("You are eligible to vote and work.")
else:
print("You are not eligible to vote or work.")
Explanation
The example is a simple eligibility check based on age. The variable age is assigned a value of 25, and the code checks whether the age falls within the range of 18 to 60 (inclusive). This example demonstrates a basic use of logical operators (and) to evaluate multiple conditions for decision-making in a Python program.
Output
You are eligible to vote and work.
Example 2: Validating User Input
username = input("Enter your username: ")
password = input("Enter your password: ")
if username == "admin" and password == "admin@123":
print("Login successful.")
else:
print("Invalid username or password.")
Explanation
This example captures user input for a username and password. It then checks whether the entered username is "admin" and the password is "admin@123". If both conditions are met, the program prints "Login successful." Otherwise, it prints "Invalid username or password." This example demonstrates a basic use of logical operators (and).
Output
Enter your username: admin
Enter your password: admin@123
Login successful.
Example 3: Temperature Check
temperature = 30
if temperature > 30 or temperature < 10:
print("Extreme temperature detected. Take precautions.")
else:
print("Temperature is within the normal range.")
Explanation
This Python code snippet in the Python Editor checks the value of the variable temperature to determine if it falls outside the normal temperature range. This code showcases the use of logical operators (or) in a conditional statement to make decisions based on specific temperature conditions.
Output
Temperature is within the normal range.
Summary
In this article, we understood that Logical operators are vital components in Python to create effective decision-making logic. Understanding and, or, and not empowers you to write more intelligent and understandable Python code, especially when writing an if statement with and or not in Python.
Next Step:
Read our data types in Python article to learn about more data types in Python.
If you want to make a career in Python, consider joining our Python Certification Course.
FAQs
Take our Python skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.