Please enable JavaScript to use CodeHS

Boolean Expressions in Python

An introduction to Boolean expressions in Python.

By Evelyn Hunter

A Boolean value is either true or false. In Python, the two Boolean values are True and False, and the Python type is bool.

Boolean expression is an expression that evaluates to produce a result which is a Boolean value. For example, the operator == tests if two values are equal. It produces (or yields) a Boolean value:

The == operator is one of six common comparison operators which all produce a bool result.

x == y               # Produce True if ... x is equal to y
x != y               # ... x is not equal to y
x > y                # ... x is greater than y
x < y                # ... x is less than y
x >= y               # ... x is greater than or equal to y
x <= y               # ... x is less than or equal to y

Be careful--one common mistake is to use a single equal sign (=) which is an assignment operator instead of the double equal sign (==) which is a comparison operator.

Try it!

This program asks for the temperature and stores it in the variable temperature

If it is colder than 60 degrees, you need a jacket. Write a boolean expression that evaluates if you need to wear a jacket or not, and then prints the boolean value.