Definition
An expression that evaluates to a Boolean value: True or False. They form the basis of decision-making in a program.
Why It Matters
They are the fundamental ‘decision gates’ that allow software to respond dynamically to its environment rather than following a static path.
Core Concepts
- Comparison (Relational) Operators:
==(Equal to): True if values are identical.!=(Not equal to): True if values are different.>,<,>=,<=: Standard numeric comparison.
- Assignment vs. Equality:
=(Assignment): “Put the value on the right into the variable on the left.”==(Equality): “Ask if the value on the left is the same as the value on the right.”
- Logical Operator Truth Tables:
True and True→True(Everything elseFalse)False or False→False(Everything elseTrue)
- Mixed Operators: Python evaluates math operators first, then comparison operators, then Boolean operators (
not>and>or).
age = 18
is_member = True
# Simple comparison test
print(age >= 18) # True
# Complex conditional test using logical operators
if age >= 18 and is_member:
print("Access granted to VIP area.")