Logical Operators

Logical operators are used to combine multiple conditions in Python. They return a Boolean result, which means the output will be either True or False. Logical operators are mainly used in decision making statements such as if conditions and loops.

Understanding logical operators is important for building programs that require multiple conditions to be checked at the same time.

AND OPERATOR

The and operator returns True only if both conditions are True. If one condition is False, the result will be False.

Example:

x = 10
print(x > 5 and x < 20)

Output will be True because both conditions are correct.

If one condition becomes False:

print(x > 5 and x > 20)

Output will be False.

OR OPERATOR

The or operator returns True if at least one condition is True. It only returns False when both conditions are False.

Example:

x = 10
print(x > 5 or x > 20)

Output will be True because one condition is True.

If both conditions are False:

print(x < 5 or x > 20)

Output will be False.

NOT OPERATOR

The not operator reverses the Boolean result. If the condition is True, it becomes False. If it is False, it becomes True.

Example:

x = 10
print(not x > 5)

Since x > 5 is True, the result will be False.

USING LOGICAL OPERATORS IN IF STATEMENTS

Logical operators are commonly used inside conditional statements.

Example:

age = 25
if age > 18 and age < 60:
print(“You are eligible”)

This checks multiple conditions at the same time.

WHY LOGICAL OPERATORS ARE IMPORTANT

Logical operators allow programs to handle complex decision making. They help in validating user input, checking multiple rules, and controlling program flow efficiently.

Mastering logical operators enables you to build smarter and more dynamic Python applications.

Home » PYTHON FUNDAMENTALS (PYF) > Operators > Logical Operators