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:

OperatorMeaning
<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.

Runs locally with Python in your browser.

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 b is true only when both conditions are true.
  • a or b is true when at least one condition is true.
  • not a reverses the boolean value.

Do Not Confuse Assignment and Comparison

Do not write a single = when you mean to compare:

Exercise: Read the comparison

What value does this expression produce?

7 != 7
Choose one

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.