Python Operators: Compute Values with Confidence
Introduction
In this chapter, you will learn Python operators, which are symbols used to perform calculations and comparisons. Operators are essential because they turn raw values into useful results for logic, decision-making, and data processing. Once you understand operators, your code starts to feel much more powerful.
Prerequisites
- Python
3.10+installed - Basic understanding of variables
- Ability to run Python files in terminal or IDE
What Is an Operator
An operator tells Python what action to perform between values.
Simple metaphor:
- Values are ingredients.
- Operators are cooking actions (
+mix,*scale,==compare). - The result is your final dish.
Example:
# Add two numbers
total = 3 + 5
print(total)1) Arithmetic Operators
Use these for math operations.
Common arithmetic operators:
+addition-subtraction*multiplication/division (returns float)//floor division%modulo (remainder)**exponent
Example:
# Basic arithmetic examples
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000Tip
Practical Hint
Use % to check odd/even quickly: n % 2 == 0 means even.
2) Assignment Operators
Assignment operators update values.
Common assignment operators:
=assign value+=add and assign-=subtract and assign*=multiply and assign/=divide and assign
Example:
# Update score step by step
score = 50
score += 10
score -= 5
score *= 2
print(score) # 1103) Comparison Operators
Comparison operators return True or False.
Common comparison operators:
==equal!=not equal>greater than<less than>=greater than or equal<=less than or equal
Example:
# Compare two values
x = 8
y = 12
print(x == y) # False
print(x != y) # True
print(x < y) # True
print(x >= y) # False4) Logical Operators
Logical operators combine conditions.
Common logical operators:
andboth conditions must be trueorat least one condition is truenotreverse boolean value
Example:
# Decide if user can enter
age = 20
has_ticket = True
can_enter = age >= 18 and has_ticket
print(can_enter) # TrueWarning
Do not confuse = and ==.
= assigns a value, == compares two values.
5) Operator Precedence (Execution Order)
Python evaluates some operators before others.
Common order (simplified):
()parentheses***,/,//,%+,-- comparisons (
==,>, etc.) not,and,or
Example:
# Multiplication happens before addition
result_1 = 2 + 3 * 4 # 14
# Parentheses change order
result_2 = (2 + 3) * 4 # 20
print(result_1, result_2)Mini Practice
Try this practice script:
# Input two numbers
num1 = 15
num2 = 4
# Arithmetic
print("sum:", num1 + num2)
print("difference:", num1 - num2)
print("product:", num1 * num2)
print("division:", num1 / num2)
print("remainder:", num1 % num2)
# Comparison
print("num1 > num2:", num1 > num2)
# Logical
print("both positive:", num1 > 0 and num2 > 0)Common Beginner Mistakes
Mistake 1: Division result surprise
/ always returns a float in Python, even for clean division.
Mistake 2: Forgetting parentheses
Expressions may run in unexpected order without parentheses.
Mistake 3: Using = in a comparison
In conditions, use ==, not =.
FAQ
Which operators should I memorize first?
Start with arithmetic (+ - * /), comparison (== > <), and logical (and or not). These cover most beginner tasks.
Why does 10 / 2 return 5.0 instead of 5?
Because / is true division in Python and returns float values.
When should I use // instead of /?
Use // when you need integer-style division (floor result), such as page counts or grouping items.
Is operator precedence important for beginners?
Yes. Even simple formulas can break if order is misunderstood. Parentheses make intent clear and reduce mistakes.