Concept Flow - Comparison operators
Start with two values
Apply comparison operator
Evaluate condition: True or False?
Use result in program
End
Comparison operators check how two values relate and give True or False as result.
Jump into concepts and practice - no test required
a = 5 b = 3 result = a > b print(result)
| Step | Expression | Evaluation | Result |
|---|---|---|---|
| 1 | a = 5 | Assign 5 to a | a = 5 |
| 2 | b = 3 | Assign 3 to b | b = 3 |
| 3 | result = a > b | Is 5 greater than 3? | True |
| 4 | print(result) | Print the value of result | True |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| a | undefined | 5 | 5 | 5 | 5 |
| b | undefined | undefined | 3 | 3 | 3 |
| result | undefined | undefined | undefined | True | True |
Comparison operators compare two values and return True or False. Common operators: >, <, >=, <=, ==, != Used to check conditions in programs. Example: 5 > 3 is True. They do not return numbers, only boolean results.
== is used to check if two values are equal in Python.= is for assignment, != means not equal, and > means greater than.x is not equal to 10 in Python?!= is the correct operator for 'not equal'.<> is not valid in Python, =! and !== are syntax errors.print(5 > 3 and 2 == 2)
if 4 =< 5:
print("Yes")=< is not valid in Python; the correct operator is <=.n is between 10 and 20 inclusive. Which expression correctly uses comparison operators in Python?