5 min read
Operators in Python
Learn all the symbols to work with data in Python
What You'll Learn
- Math operators (+, -, *, /)
- Comparison operators (==, !=, >, <)
- Logic operators (and, or, not)
- Assignment operators (=, +=, -=)
Math Operators
These do calculations:
Basic Math
code.py
# Addition
result = 10 + 5
print(result) # 15
# Subtraction
result = 10 - 5
print(result) # 5
# Multiplication
result = 10 * 5
print(result) # 50
# Division
result = 10 / 5
print(result) # 2.0Special Math
code.py
# Power (multiply by itself)
result = 2 ** 3
print(result) # 8 (2×2×2)
# Floor Division (no decimals)
result = 10 // 3
print(result) # 3 (removes decimal)
# Remainder (what's left over)
result = 10 % 3
print(result) # 1 (10÷3 = 3 remainder 1)Comparison Operators
Compare values, get True or False:
code.py
age = 25
# Equal to
print(age == 25) # True
print(age == 30) # False
# Not equal to
print(age != 30) # True
# Greater than
print(age > 20) # True
# Less than
print(age < 30) # True
# Greater or equal
print(age >= 25) # True
# Less or equal
print(age <= 25) # TrueLogic Operators
Combine True/False values:
AND (both must be True)
code.py
age = 25
has_id = True
can_enter = age >= 18 and has_id
print(can_enter) # Truecode.py
age = 16
has_id = True
can_enter = age >= 18 and has_id
print(can_enter) # False (age too low)OR (at least one True)
code.py
is_weekend = True
is_holiday = False
can_rest = is_weekend or is_holiday
print(can_rest) # TrueNOT (opposite)
code.py
is_raining = False
is_sunny = not is_raining
print(is_sunny) # TrueAssignment Operators
Give values to variables:
Simple Assignment
code.py
x = 10
name = "John"Shortcut Assignments
Instead of writing long code, use shortcuts:
code.py
# Add and assign
score = 10
score += 5 # Same as: score = score + 5
print(score) # 15
# Subtract and assign
score = 20
score -= 5 # Same as: score = score - 5
print(score) # 15
# Multiply and assign
price = 10
price *= 2 # Same as: price = price * 2
print(price) # 20
# Divide and assign
total = 100
total /= 4 # Same as: total = total / 4
print(total) # 25.0Practice Examples
Example 1: Shopping cart
code.py
total = 0
# Add items
total += 50 # First item
total += 30 # Second item
total += 20 # Third item
print("Total:", total)
# Shows: Total: 100Example 2: Check discount
code.py
price = 100
has_coupon = True
is_member = True
# Get discount if coupon OR member
gets_discount = has_coupon or is_member
print("Gets discount:", gets_discount)
# Shows: Gets discount: TrueExample 3: Age verification
code.py
age = 20
has_permission = True
# Can enter if 18+ AND has permission
can_enter = age >= 18 and has_permission
print("Can enter:", can_enter)
# Shows: Can enter: TrueExample 4: Even or odd
code.py
number = 7
is_even = number % 2 == 0
print("Is even:", is_even)
# Shows: Is even: FalseString Operators
Work with text:
code.py
# Combine strings
first = "Hello"
last = "World"
full = first + " " + last
print(full) # Hello World
# Repeat strings
laugh = "ha" * 3
print(laugh) # hahaha
# Check if text is inside
sentence = "I love Python"
has_python = "Python" in sentence
print(has_python) # TrueOperator Order
Python follows math rules (PEMDAS):
code.py
result = 2 + 3 * 4
print(result) # 14 (multiply first)
result = (2 + 3) * 4
print(result) # 20 (parentheses first)Order:
- Parentheses ()
- Power **
- Multiply *, Divide /, Remainder %
- Add +, Subtract -
Common Mistakes
Mistake 1: Using = instead of ==
code.py
# Wrong (this assigns, doesn't compare)
if age = 18:
# Correct (this compares)
if age == 18:Mistake 2: Forgetting parentheses
code.py
# Without parentheses
result = 10 + 5 * 2
print(result) # 20 (multiply first)
# With parentheses
result = (10 + 5) * 2
print(result) # 30Quick Reference
Math:
code.py
+ # Add
- # Subtract
* # Multiply
/ # Divide
** # Power
// # Floor division
% # RemainderComparison:
code.py
== # Equal
!= # Not equal
> # Greater
< # Less
>= # Greater or equal
<= # Less or equalLogic:
code.py
and # Both True
or # At least one True
not # OppositeAssignment:
code.py
= # Assign
+= # Add and assign
-= # Subtract and assign
*= # Multiply and assign
/= # Divide and assignSummary
- Math operators do calculations
- Comparison operators compare values
- Logic operators combine True/False
- Assignment operators give values to variables
- Use () to control order of operations
What's Next?
Now let's learn how to convert between different data types!