Boolean Expressions in Python
Boolean expressions in Python evaluate to either True
or False
. They are widely used in conditional statements, loops, and logical operations.
Syntax
# Boolean values
True
False
# Boolean expressions with comparison operators
x == y # Equal to
x != y # Not equal to
x > y # Greater than
x < y # Less than
x >= y # Greater than or equal to
x <= y # Less than or equal to
# Boolean expressions with logical operators
A and B # True if both A and B are True
A or B # True if at least one of A or B is True
not A # Negates A (True becomes False, False becomes True)
Boolean Values in Python
Python has two Boolean values: True
and False
. These are case-sensitive, so always use an uppercase T
and F
.
Return Value
Boolean expressions return either True
or False
depending on the conditions evaluated.
Examples
1. Basic Boolean Values
In this example, we will print the two Boolean values available in Python.
Python provides True
and False
as built-in constants. Let’s print them:
# Printing Boolean values
print(True)
print(False)
Output:
True
False
When you run this code, Python simply prints the Boolean values True
and False
.
2. Boolean Expressions with Comparison Operators
Comparison operators return a Boolean value based on the relationship between two values.
Here, we compare two numbers using different comparison operators:
x = 10
y = 20
# Comparison operations
print(x == y) # Is x equal to y?
print(x != y) # Is x not equal to y?
print(x < y) # Is x less than y?
print(x > y) # Is x greater than y?
print(x <= y) # Is x less than or equal to y?
print(x >= y) # Is x greater than or equal to y?
Output:
False
True
True
False
True
False
Each line evaluates a condition. Since x = 10
and y = 20
:
x == y
returnsFalse
because 10 is not equal to 20.x != y
returnsTrue
because 10 is indeed not equal to 20.x < y
returnsTrue
because 10 is less than 20.x > y
returnsFalse
because 10 is not greater than 20.x <= y
returnsTrue
because 10 is less than or equal to 20.x >= y
returnsFalse
because 10 is not greater than or equal to 20.
3. Boolean Expressions with Logical Operators
Logical operators allow combining multiple Boolean conditions.
Python provides three logical operators:
and
– ReturnsTrue
if both conditions areTrue
.or
– ReturnsTrue
if at least one condition isTrue
.not
– Negates a Boolean value.
a = True
b = False
# Logical operations
print(a and b) # Both must be True
print(a or b) # At least one must be True
print(not a) # Negates the value of a
Output:
False
True
False
Here’s what happens:
a and b
returnsFalse
becauseb
isFalse
, and both values must beTrue
forand
to returnTrue
.a or b
returnsTrue
because at least one value (a
) isTrue
.not a
returnsFalse
becausea
isTrue
, andnot
negates the value.
4. Boolean Expressions in Conditional Statements
Boolean expressions are commonly used in if
statements to control program flow.
x = 15
if x > 10:
print("x is greater than 10")
else:
print("x is 10 or less")
Output:
x is greater than 10
The condition x > 10
is True
, so the first block executes, printing the message.