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
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33.333...
//Floor Division10 // 33
%Modulus10 % 31
**Exponent2 ** 38
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 to5 == 5True
!=Not equal5 != 3True
>Greater than10 > 5True
<Less than3 < 2False
>=Greater or equal5 >= 5True
<=Less or equal4 <= 3False
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 = 5x = 5
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2
//=x //= 3x = x // 3
%=x %= 3x = x % 3
**=x **= 2x = 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
Lowestor

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, is for identity checks
  • Use in for membership testing instead of manual loops
  • Use augmented assignment (+=) for cleaner code

Common Mistakes

  • Confusing = (assignment) with == (comparison)
  • Integer division surprise: 7 / 2 gives 3.5, use 7 // 2 for 3
  • Using is instead of == for value comparison (works for small ints but fails for larger values)
  • Forgetting operator precedence: 2 + 3 * 4 is 14, not 20

Practice Exercises

1

Write a program that takes two numbers and prints the result of all 7 arithmetic operations (+, -, *, /, //, %, **).

2

Create a tip calculator: take a bill amount and tip percentage, calculate the tip and total.

3

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.