Chapter 18 of 20

Logical Operators in Python

Sometimes a program needs to check more than one condition at the same time.

For example:

A student can enter an exam if they have a valid ID and their registration is complete.

Python provides logical operators to combine conditions and make these kinds of decisions.

What are Logical Operators?

Logical operators are used to combine or modify conditions and produce a True or False result.

Python has three logical operators:

Operator

Meaning

and

All conditions must be true

or

At least one condition must be true

not

Reverses the result

Let's understand each one.

1. and Operator

The and operator returns True only when both conditions are true.

age = 20
has_id = True

print(age >= 18 and has_id)

Output:

True

Both conditions are true:

age >= 18  → True
has_id     → True

So the complete result is True.

If even one condition is false:

age = 16
has_id = True

print(age >= 18 and has_id)

Output:

False

2. or Operator

The or operator returns True if at least one condition is true.

age = 16
has_permission = True

print(age >= 18 or has_permission)

Output:

True

The age condition is false, but has_permission is true, so the overall result is true.

It returns False only when all conditions are false.

3. not Operator

The not operator reverses a Boolean value.

is_logged_in = True

print(not is_logged_in)

Output:

False

Because:

not True → False
not False → True

Using Logical Operators with if

Logical operators become especially useful when combined with if statements.

For example:

age = 25
has_ticket = True

if age >= 18 and has_ticket:
    print("You can enter.")

Both conditions must be true for the message to be displayed.

Combining and and or

You can combine multiple logical operators.

age = 20
has_ticket = False
is_vip = True

if (age >= 18 and has_ticket) or is_vip:
    print("Entry allowed")

Here, the first group is false, but is_vip is true, so the complete condition is true.

Using parentheses makes complicated conditions much easier to understand.

Logical Operators with Comparisons

Most of the time, you'll use logical operators together with comparison operators.

marks = 85
attendance = 80

if marks >= 40 and attendance >= 75:
    print("Student passed")

Here:

marks >= 40       → True
attendance >= 75  → True

Therefore:

True and True → True

Truth Table

Here's a simple way to understand the three operators:

and

A

B

A and B

True

True

True

True

False

False

False

True

False

False

False

False

or

A

B

A or B

True

True

True

True

False

True

False

True

True

False

False

False

not

A

not A

True

False

False

True

Python's Short-Circuit Evaluation

Python doesn't always evaluate every condition.

For example:

age = 15

if age >= 18 and expensive_check():
    print("Allowed")

Since age >= 18 is already False, Python doesn't need to evaluate the second part of the and expression.

Similarly, with or, if the first condition is already True, Python can skip the remaining conditions.

This behavior is called short-circuit evaluation.

A Practical Example

Let's create a simple login check:

username = "john"
password = "1234"

if username == "john" and password == "1234":
    print("Login successful")
else:
    print("Invalid username or password")

Output:

Login successful

Here, and makes sure both the username and password are correct.

Logical operators are extremely useful because they allow your programs to handle multiple conditions and make smarter decisions instead of checking only one condition at a time.