Operators
Lesson opener
In one line: Operators do arithmetic, comparison, and logic on the numbers you already stored — 914.35 / 9 is an average order, revenue >= 1000 is a yes/no, and joins two yes/no checks.
Why it matters: A pasted calculator result cannot be rerun. An expression on named fields from data/customers.csv can. This lesson is the arithmetic you will put inside the if chains from Conditionals.
Before this lesson: Data types — you know int, float, str, and bool. Variables — you can load one customer into names.
Practitioner focus: C0001 has 9 orders and ₹914.35 lifetime revenue. Every printed number below is from that row or from all 300 rows. Do not use
==to decide two floats are the same amount of money.
What you will learn
By the end of this lesson you will be able to:
- Compute an average order value with
/and a floor with//. - Compare a real revenue to a threshold (
>= 1000) and printTrueorFalse. - Join two checks with
and/or/not. - Reassign with
+=when a total grows. - Avoid
0.1 + 0.2 == 0.3— usemath.isclose. - Read C0003 as Gold and above ₹1,000, and say both facts in one line.
Worked example: C0001's order math
Step 1 — Divide, floor, remainder. Average order is revenue ÷ orders. Floor division drops the paise. Remainder after tickets % 5 is 2 because C0001 has 2 tickets.
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.iloc[0]
revenue = float(row["total_revenue"])
orders = int(row["total_orders"])
tickets = int(row["support_tickets"])
print(round(revenue / orders, 2))
print(int(revenue) // orders)
print(tickets % 5)Expected output:
101.59
101
2/ always gives a float. // gives a whole number. % is the leftover after groups of 5 — useful later for "every fifth ticket escalates."
Step 2 — Comparisons are True or False. C0001 is under ₹1,000 and not Gold. C0003 is Gold and over ₹1,000.
import pandas as pd
df = pd.read_csv("data/customers.csv")
c1 = df.iloc[0]
c3 = df.loc[df["customer_id"] == "C0003"].iloc[0]
print(float(c1["total_revenue"]) >= 1000)
print(c1["segment"] == "Gold")
print(float(c3["total_revenue"]) >= 1000)
print(c3["segment"] == "Gold")Expected output:
False
False
True
TrueStep 3 — and needs both. C0003 is the row that is Gold and over ₹1,000.
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.loc[df["customer_id"] == "C0003"].iloc[0]
ok = (row["segment"] == "Gold") and (float(row["total_revenue"]) >= 1000)
print(ok)Expected output:
Trueor is enough if either signal should pass. not flips a boolean.
Step 4 — Do not trust float ==. This is independent of the CSV. Binary floats cannot hold 0.1 exactly.
import math
print(0.1 + 0.2 == 0.3)
print(math.isclose(0.1 + 0.2, 0.3))Expected output:
False
TrueFor money stored as rupees-and-paise in this file, compare with >= / <= or math.isclose, not ==.
Step 5 — += grows a total. Sum support tickets for the first five customers (C0001–C0005). That sum is 10.
import pandas as pd
df = pd.read_csv("data/customers.csv")
total = 0
for n in df.head(5)["support_tickets"]:
total += int(n)
print(total)Expected output:
10total += n means total = total + n. Same name, new value — the reassignment from Variables.
Pause and think
Q1. Why is C0001's revenue >= 1000 False when 914.35 looks "almost 1,000"?
Almost is not over. The rule is a cut, not a vibe. 914.35 is band C in Conditionals.
Q2. What does and return if the first side is False?
False, and Python does not evaluate the second side (short-circuit). Put the cheap check first when you can.
Q3. When is == the wrong tool for two floats?
When they came from division or from 0.1-style decimals. Use
iscloseor a threshold.
Guided practice: average order for C0003
C0003 has 19 orders and ₹1,988.38 revenue. Print the average rounded to 2 decimals.
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.loc[df["customer_id"] == "C0003"].iloc[0]
print(round(float(row["total_revenue"]) / int(row["total_orders"]), 2))Expected output:
104.65Independent practice: how many Gold?
Deliverable: print how many of the 300 customers have segment == "Gold".
import pandas as pd
df = pd.read_csv("data/customers.csv")
print(int((df["segment"] == "Gold").sum()))Expected output:
62Common mistake
The deliberate mistake
Symptom:
if average == 101.59:never fires even though you just printed 101.59.Diagnosis: the printed value was rounded; the raw float is not bit-equal to the literal.
Correction: compare with
>=/<=, ormath.isclose, or round both sides on purpose.Prevention: treat
==as a tool for ids and segments ("Gold"), not for rupee averages.
Where this connects
What you can now do:
- Divide, floor, and remainder on real order numbers.
- Combine two facts with
and. - Refuse float
==for money.
Next lesson: Strings. Customer ids are text. Slicing C0001 is the next operator family.