Sub topic 1
Conditions: comparison operators and boolean expressions
Every if statement depends on a condition. A condition is an expression that Python evaluates to decide which code runs. This chapter covers the comparison operators that build most conditions and what they return.
Prerequisites
- You can run a Python script or use the interactive interpreter.
- You know the basic types:
int,float,str,bool,list,dict, andNone. - You can call
print()and read a simple function definition.
Comparisons produce booleans
A comparison operator compares two values and returns a bool, which is either True or False. The if statement uses that result to choose a branch.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `==` | equal to | `3 == 3` | `True` |
| `!=` | not equal to | `3 != 4` | `True` |
| `<` | less than | `2 < 1` | `False` |
| `>` | greater than | `5 > 2` | `True` |
| `<=` | less than or equal | `4 <= 4` | `True` |
| `>=` | greater than or equal | `3 >= 5` | `False` |
age = 20
print(age >= 18)
print(age == 18)
print(type(age >= 18))This prints True, False, and <class 'bool'>. A comparison is an ordinary expression, so you can store its result in a variable and use it later:
age = 20
is_adult = age >= 18
print(is_adult)Comparing different kinds of values
Numbers compare by value, so an int and a float can be equal:
print(1 == 1.0)
print('1' == 1)The first line prints True. The second prints False, because Python does not convert a string to a number for you. Using < or > across incompatible types is worse. '1' < 1 raises a TypeError.
Strings compare character by character using Unicode code points. Uppercase letters sort before lowercase ones, so case matters:
print('apple' < 'banana')
print('Zebra' < 'apple')
print('abc' == 'ABC')
print('abc' == 'ABC'.lower())This prints True, True, False, and True. To compare text without regard to case, normalize both sides first, for example with .lower().
Floats need care
Floating-point numbers are stored in binary, so many decimal values are only approximations. An exact == test can fail in surprising ways:
import math
print(0.1 + 0.2 == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))The first line prints False and the second prints True. When you compare computed floats, use math.isclose() instead of ==.
Common errors
- Using
=instead of==. A single=assigns a value. Writingif age = 18:is aSyntaxError, which Python reports when it parses the file. - Forgetting that comparisons return
bool. Writingif (age >= 18) == True:works, butif age >= 18:is shorter and clearer. - Comparing mismatched types. Input from
input()is always a string. Convert it withint()before comparing it to a number. - Exact float equality. Use
math.isclose()for computed values.
Later chapters combine these building blocks with and, or, and not, and cover how Python treats non-boolean values as conditions.
Exercise
What do print(10 != 10.0) and print('10' == 10) output, and why?
Show answer
They print False and False. The number 10 equals 10.0 in value, so != is False. A string is never equal to an integer, so the second comparison is also False. Neither raises an error, because == and != accept any types.
Exercise
Why does if total = 100: fail, and how do you fix it?
Show answer
A single = is assignment, not comparison, so Python raises a SyntaxError. Use == to test equality: if total == 100:.