Boolean Values and Expression

What are Boolean Values?

Boolean values represent one of two values: True or False.

What is a Boolean Expression?

Boolean expression is an expression that evaluates to produce a result which is a Boolean value using Comparison Operators, that compares two operands and evaluates whether the expression is True or False.

For example, the operator (==) tests if two values are equal. It produces (or yields) a Boolean value:

print(5 == (3 + 2)) 
True

Boolean Expressions Using Comparison Operators

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

When you compare two values, the expression is evaluated and Python returns the Boolean answer:

print(10 > 9)  
print(10 == 9)  
print(10 < 9)
Output
True
False
False

Evaluating Boolean Expressions

Python will evaluate any combination of boolean expression into a single boolean value.

Short-Circuit Boolean Evaluations

Short-circuiting is where an expression is stopped being evaluated as soon as its outcome is determined. This often occurs when combining conditional statements with logical operators.

Example:

# Short-circuit

if a == b or c == d or e == f:
	<statement>
	...

If a == b is true, then c == d and e == f are never evaluated at all, because the expression's outcome has already been determined.

Powered by Forestry.md