Operators
Week 1 — Python Basics Revision
Introduction
Operators are symbols that perform operations on values. Adding two numbers, comparing ages, assigning values to variables — all use operators. Python provides several types of operators, and understanding them is essential for writing any meaningful program.
Arithmetic Operators
Python supports all standard arithmetic operations, plus a few extras like floor division and exponentiation.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333... |
| // | Floor Division | 10 // 3 | 3 |
| % | Modulus | 10 % 3 | 1 |
| ** | Exponent | 2 ** 3 | 8 |
a = 15
b = 4
print(f"{a} + {b} = {a + b}") # 19
print(f"{a} - {b} = {a - b}") # 11
print(f"{a} * {b} = {a * b}") # 60
print(f"{a} / {b} = {a / b}") # 3.75
print(f"{a} // {b} = {a // b}") # 3
print(f"{a} % {b} = {a % b}") # 3
print(f"{a} ** {b} = {a ** b}") # 50625
Comparison Operators
Comparison operators evaluate two values and return a boolean result — either True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal | 5 != 3 | True |
| > | Greater than | 10 > 5 | True |
| < | Less than | 3 < 2 | False |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 4 <= 3 | False |
age = 20
print(age >= 18) # True
print(age == 21) # False
print(age != 20) # False
Assignment Operators
Assignment operators store values in variables. Augmented assignment operators combine an operation with assignment in a single step.
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 3 | x = x + 3 |
| -= | x -= 2 | x = x - 2 |
| *= | x *= 4 | x = x * 4 |
| /= | x /= 2 | x = x / 2 |
| //= | x //= 3 | x = x // 3 |
| %= | x %= 3 | x = x % 3 |
| **= | x **= 2 | x = x ** 2 |
score = 100
score += 10 # 110
score -= 5 # 105
score *= 2 # 210
print(score) # 210
Membership Operators
in and not in check if a value exists in a sequence such as a list, string, or tuple.
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True
name = "Python"
print("th" in name) # True
Identity Operators
is and is not check if two variables refer to the same object in memory, not just the same value.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True (same values)
print(a is b) # False (different objects)
print(a is c) # True (same object)
Operator Precedence
When an expression has multiple operators, Python follows a specific order of evaluation. Operators higher in the table are evaluated first.
| Priority | Operators |
|---|---|
| Highest | ** |
| *, /, //, % | |
| +, - | |
| ==, !=, >, <, >=, <= | |
| not | |
| and | |
| Lowest | or |
Tip: Use parentheses to make the order of evaluation explicit. It improves readability and prevents subtle bugs.
result = 2 + 3 * 4 # 14 (not 20)
result = (2 + 3) * 4 # 20
Real-World Example
Here is a simple shopping cart calculation that uses arithmetic, assignment, and f-string formatting together.
price = 49.99
quantity = 3
discount_percent = 10
subtotal = price * quantity
discount = subtotal * discount_percent / 100
total = subtotal - discount
print(f"Subtotal: ${subtotal:.2f}")
print(f"Discount: -${discount:.2f}")
print(f"Total: ${total:.2f}")
OOP Connection
In OOP, you can redefine how operators work for your own classes. This is called operator overloading — you will learn it in Week 5. For example, you could make the + operator add two BankAccount balances together.
Best Practices
- Use parentheses to make complex expressions clear
- Use
==for value comparison,isfor identity checks - Use
infor membership testing instead of manual loops - Use augmented assignment (
+=) for cleaner code
Common Mistakes
- Confusing
=(assignment) with==(comparison) - Integer division surprise:
7 / 2gives3.5, use7 // 2for3 - Using
isinstead of==for value comparison (works for small ints but fails for larger values) - Forgetting operator precedence:
2 + 3 * 4is 14, not 20
Practice Exercises
Write a program that takes two numbers and prints the result of all 7 arithmetic operations (+, -, *, /, //, %, **).
Create a tip calculator: take a bill amount and tip percentage, calculate the tip and total.
Check if a number is even or odd using the modulus operator (%).
Summary
You now know all the operators Python offers — arithmetic, comparison, assignment, membership, and identity. You also understand how operator precedence determines the order of evaluation. Next, we will learn how to get input from users and display formatted output.