Conditions and Comparisons
A condition is an expression used for a decision. In Python, a condition usually
produces a boolean value: True or False.
Output:
True
False
Comparison Operators
You have seen these before, but now they get a job:
| Operator | Meaning |
|---|---|
< | less than |
<= | less than or equal to |
> | greater than |
>= | greater than or equal to |
== | equal value |
!= | not equal value |
Each comparison produces a boolean.
Comparisons produce booleans
Run the code and inspect each comparison result.
Ready to run.
Assignment Is Not Comparison
This assigns a value:
score = 8
This compares values:
score == 8
The difference matters. A branch needs a condition, so it needs an expression that can be read as true or false.
Combining Conditions
You can combine conditions with and, or, and not:
Use these only when the combined condition remains easy to read.
a and bis true only when both conditions are true.a or bis true when at least one condition is true.not areverses the boolean value.
Do Not Confuse Assignment and Comparison
Do not write a single = when you mean to compare:
What value does this expression produce?
7 != 7
Select one choice, then check.
HintRead the operator in words
!= means “is not equal to.” Ask whether seven is different from seven.
SolutionThe comparison is false
Both sides are 7, so they are equal. The claim that they are not equal is
therefore False.
Conditions Decide the Path
A branch or loop often begins with a comparison. If you can predict the boolean, you can predict the path.