Data work is repetitive: for every row, for every product. A loop repeats automatically, and a function lets you call the same code again.
for loop — act on each item:
for price in [20000, 12000, 8000]:
usd = price / 4100
print(price, "→", round(usd, 2))
if — decide:
if price > 10000:
print("ថ្លៃ")
else:
print("ថោក")
function — code you call again:
def to_usd(khr, rate=4100):
return khr / rate
to_usd(20000) # ~4.88
to_usd(12000) # ~2.93
A real workflow: read a row, compute, add:
total = 0
for row in rows:
total += row["price_khr"] * row["qty"]
Takeaways:
- for = repeat; if = decide; def = call again
- Counting starts at 0; code inside a loop is indented
- A function makes code reusable and readable