Boolean Operators in Python



The Boolean operators in Python return two values, either true or false. These operators are mostly used in conditional statements, loops, and logical expressions. Python has a built-in Boolean datatype that returns Boolean values based on comparisons or logical evaluations between operands.

Boolean Operators in Python

There are three main Boolean operators in Python: OR, AND, and NOT. These operators return Boolean values based on the operands.

OR Operator in Python

The OR operator in Python returns true if one of the operands is true and false if both operands are false. This operator needs two values or operands to perform the operation and return the result.

Example of the OR Operator

The following is an example of an OR operator in Python. In the example below, both variables have been assigned Boolean values, and based on these values, the OR operator prints the resultant text.

is_hungry = False
is_thirsty = True

if is_hungry or is_thirsty:
    print("Time for a snack or a drink!")
else:
    print("All good for now!")
Following is the output of the above programTime for a snack or a drink!

AND Operator in Python

The AND operator in Python also needs two operands. This operator returns true if both operands are true and false if either of the operands is false.

Example of the AND Operator

The following is an example of an AND operator in Python. In the example below, we are checking the eligibility of a person to vote. One variable has been assigned a Boolean value, and the other variable is performing a comparison operation to check if the age is above 18. Based on these values, the AND operator prints the resultant text.

age = 22
has_voter_id = True

if age >= 18 and has_voter_id:
    print("You are eligible to vote.")
else:
    print("Sorry, you are not eligible to vote.")

Following is the output of the above program -

You are eligible to vote.

NOT Operator in Python

The NOT operator in Python reverses the value of a Boolean expression. If the value is true, it will convert it to false, and if the value is false, it will convert it to true.

Example of the NOT Operator

The following is an example of the Not operator in Python. In the example below, a variable has been assigned a Boolean value. The Not operator reverses that value, and based on it, the Not operator prints the resultant text.

red_light = True

if not red_light:
    print("You can go!")
else:
    print("Stop! Wait for the green light.")

Following is the output of the above program -

Stop! Wait for the green light.
Updated on: 2025-04-18T15:12:10+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements