Python Basics
Python Conditions & Comparison Operators
1. What is a Condition?
- An expression that evaluates to
TrueorFalse - Built using comparison operators applied to operands
- Essential for
ifstatements and conditionals (covered in next lessons)
con = 2 == 3 # False (2 is not equal to 3)
con = 3 == 3 # True
Important distinction: -
=→ assignment operator (stores a value in a variable) -==→ comparison operator (checks if two values are equal)
2. The 6 Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
Equal to | 3 == 3 |
True |
!= |
Not equal to | 3 != 4 |
True |
< |
Less than | 5 < 9 |
True |
> |
Greater than | 5 > 9 |
False |
<= |
Less than or equal to | 9 <= 9 |
True |
>= |
Greater than or equal to | 9 >= 10 |
False |
- All operators work with both
intandfloat - Arithmetic is evaluated before comparison
x = 9
x + 2 > 9.5 # True (11 > 9.5)
3. Comparing int and float
==between int and float compares numeric value, not type
3.0 == 3 # True (same numeric value)
3.0 != 3 # False
<,>,<=,>=also work fine between int and float
4. Comparing Strings
Equality
- Comparison is case-sensitive and space-sensitive
"hello" == "hello" # True
"hello" == "Hello" # False (capital H differs)
"hello" == "hello " # False (trailing space differs)
Less Than / Greater Than with Strings
- Strings are compared using their ASCII (ordinal) values character by character
- Compare first characters → if equal, move to next → and so on
"a" < "b" # True (97 < 98)
"a" < "B" # False (97 > 66 — lowercase > uppercase)
"AC" < "Ab" # True (A==A, then C=67 vs b=98, so AC < Ab)
Key rule: Uppercase letters always have lower ASCII values than lowercase letters
5. ASCII Values — ord() and chr()
Every character on your keyboard has a unique numeric value called its ASCII / ordinal value
ord(character) — character → number
ord('a') # 97
ord('A') # 65
ord('!') # 33
chr(number) — number → character
chr(76) # 'L'
chr(98) # 'b'
Key ASCII Reference Points
| Character | ASCII Value |
|---|---|
'A' (uppercase) |
65 |
'B' |
66 |
'a' (lowercase) |
97 |
'b' |
98 |
Uppercase always starts at 65, lowercase at 97 — so uppercase < lowercase always
6. Comparing Mixed Types
| Comparison | Result | Reason |
|---|---|---|
"6" == 6 |
False |
Different types — not equal |
"6" != 6 |
True |
Different types |
"6" < 6 |
❌ TypeError | Can't use < / > between str and int |
True == 1 |
True |
True is numerically 1 |
False == 0 |
True |
False is numerically 0 |
False == "" |
False |
Different types |
True == "True" |
False |
Different types |
Rule:
==and!=work between any types (just checks equality).<,>,<=,>=only work between compatible types (int/float with int/float, or str with str)
7. Using Variables in Conditions
x = 5
y = 9
print(x == y) # False
print(x != 9) # True
print(x < y) # True
8. String Comparison — Worked Example
str1 = "ABc"
str2 = "ABC"
str1 <= str2
# A == A → move on
# B == B → move on
# c (99) vs C (67) → c > C
# So str1 is NOT <= str2 → False
str1 > str2 # True (lowercase c > uppercase C)
9. Key Takeaways & Recap
- A condition is any expression that evaluates to
TrueorFalse =assigns,==compares — never mix them up- Six comparison operators:
==,!=,<,>,<=,>= intvsfloatcomparisons work fine — compares numeric value- String comparison is case-sensitive and uses ASCII values
- Uppercase letters (start at 65) are always less than lowercase (start at 97)
ord()gives ASCII value of a character;chr()gives character from ASCII value==/!=work across types;</>between str and int raisesTypeError