Skip to content

Boolean expression Python

  • by

Python Boolean expression is an expression that evaluates to produce a result which is a Boolean value. An if statement uses the equal operator == to test if two values are equal and produce (or yield) a Boolean value.

The Boolean values in Python are True and False, typically used to control if-statements and while-loops.

Boolean expression Python example

Simple example code that returns a True or False value.

# Is three greater than two?
print(1 > 2)

# Is the number of characters in the string "Hello" equal to one?
print(len("Hello") == 1)

# Is 5 equal to 3 + 2?
print(5 == 3 + 2)

# Is four greater than or equal to nine?
print(2 + 2 >= 10)

# Is seven not equal to twelve?
print(5 != 10)

Output:

Boolean expression Python

Boolean expression in the if else statement

my_number = 24
if my_number % 2 == 0: 
    print(my_number, "is even.")
else:
    print(my_number, "is odd.")

Python Comparison Operators

Mostly Boolean expressions are expressions that use comparison operators.

OperatorMeaning
x == yx equals y
x != yx does not equal y, x is not equal to y
x >= yx is greater than or equal to y
x <= yx is less than or equal to y

A Truth Table for Python Boolean Expressions

Expression 1ConnectorExpression 2Result
TrueandTrueTrue
FalseandTrueFalse *
TrueandFalseFalse
FalseandFalseFalse *
TrueorTrueTrue *
FalseorTrueTrue
TrueorFalseTrue *
FalseorFalseFalse

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *