Arithmetic operators
These perform basic math: +, -, *, /, % (modulus - the remainder after division), and ** (exponent).
a = 150
b = 200
c = 300
d = 10
e = 3
total = a + b + c # 650
diff = b - a # 50
rem = d % e # 1 (remainder of 10 รท 3)
div = c / d # 30.0
power = e ** 2 # 9
Comparison operators
These compare two values and always evaluate to a boolean (True or False): == (equal to), != (not equal to), >, <, >=, <=.
age = 18
print(age == 18) # True
print(age != 20) # True
print(age >= 21) # False
The most common beginner mistake here is confusing = (assignment) with == (comparison) - age = 18 sets the value; age == 18 checks it.
Logical operators
These combine multiple conditions: and (both must be true), or (at least one must be true), not (flips a boolean).
age = 20
has_id = True
if age >= 18 and has_id:
print("You can enter")
Conditionals: if, elif, else
Conditionals let a program make decisions - running different code depending on whether a condition is true:
score = 72
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: F")
Python checks each condition top to bottom and runs the first block whose condition is True, skipping the rest - elif (else-if) lets you chain as many conditions as needed, and else catches anything that didn't match.
Practical examples
Combining comparison and logical operators inside a conditional - checking eligibility against two conditions at once:
age = 19
id_present = True
if age >= 18 and id_present == True:
print("You can drive")
else:
print("Not eligible yet")
A slightly more layered example, using elif to categorize a value into ranges:
temperature = 28
if temperature > 30:
print("It's hot")
elif temperature >= 20:
print("It's warm")
else:
print("It's cold")
What I understood from this
The distinction that mattered most here was between comparison operators (which produce a boolean answer to a single question) and logical operators (which combine several of those boolean answers into one final decision). A condition like age >= 18 and has_id only reads cleanly once you see it as two separate True/False questions being joined by and - Python evaluates each side independently first, then combines them. Once that clicked, writing more complex conditions stopped being about memorizing operator symbols and became about breaking a real-world rule ("you can drive if you're old enough and you have ID") into the exact boolean pieces Python needs.
Top comments (0)