Day 11 · ~14m

Python Numbers: int, float, and Arithmetic Operators

int vs float distinction, converting strings to numbers, and why '100' + '50' = '10050'. Parse CRM data from strings to actual numbers.

student (excited)

I just built that add_to_total() function from Day 10! My running total works perfectly. Then I looked at the actual CSV our CRM exports...

teacher (neutral)

Teacher(encouraging): Let me guess — every amount is in quotes?

student (confused)

Right! It looks like "1250.50" instead of just the number. And when I tried to add them up, everything broke. Welcome to the most common Python beginner bug.

teacher (neutral)

You've just discovered the difference between strings and numbers. In Python, "1250.50" (with quotes) is text. 1250.50 (no quotes) is an actual number. Your CRM dumps everything as text because that's safe — any data format can be read as text. Your job is to convert it.

student (thinking)

So if I try to use a string in math, what happens?

teacher (neutral)

Let me show you the wrong approach first — because once you see it, you'll never forget it:

# This is the bug:
total = "100"
more = "50"
result = total + more
print(result)  # "10050" — concatenation, not addition!

Wait, what? "100" + "50" gives "10050"? That's because with strings, + means "stick them together," not "add them mathematically." You get the strings smooshed end-to-end.

student (amused)

Oh no. So my spreadsheet is lying to me. When I see "1250.50" in the CSV, it looks like a number, but Python sees pure text.

teacher (neutral)

Exactly. And here's the fix:

# Convert the string to a number first:
total = float("100")  # 100.0
more = float("50")    # 50.0
result = total + more
print(result)  # 150.0 — real addition

There are two number types in Python. An int is a whole number with no decimal point. A float is a decimal number. When you convert a string to a number, you choose which one:

int("100")      # 100 (whole number)
float("100")    # 100.0 (decimal)
float("1250.50") # 1250.5
int("1250.50")  # ERROR! Can't convert directly if there's a decimal

For your CRM data, use float() — because sale amounts almost always have cents. Then if you need a whole number later, you can use round() to clean it up.

student (thinking)

But the CSV also has dollar signs sometimes — "$1250.50". How do I handle that?

teacher (neutral)

Remove the $ first, then convert. Python strings have a .replace() method that does exactly this:

raw = "$1250.50"
cleaned = raw.replace("$", "")  # "1250.50"
amount = float(cleaned)          # 1250.5
print(amount)  # 1250.5

Think of .replace("$", "") as "find every $ and replace it with nothing" — poof, gone. You can chain these for complex strings:

raw = "$1,250.50"
cleaned = raw.replace("$", "").replace(",", "")
amount = float(cleaned)
print(amount)  # 1250.5

You can also use round() to lock in a specific number of decimal places — super useful for money, where you never want more than 2 cents:

amount = float("1250.506")
rounded = round(amount, 2)  # 1250.51 (rounded to 2 decimal places)
print(rounded)  # 1250.51
student (focused)

So in my real workflow, I'd parse every amount from the CSV, then add them up, then maybe round the final total?

teacher (neutral)

Exactly. That's the job of parse_amount() — extract one string, clean it, convert it to a float. Do that for every row in the CSV, and you've got real numbers you can add together.

student (proud)

I get it now. Strings look like numbers, but they're not. And if I forget to convert, Python treats + like concatenation instead of math. Tomorrow we get into if and else — I'm curious how that works with numbers.

teacher (neutral)

You'll be surprised — Python has a whole layer of true/false logic built underneath if statements. We'll pull back that curtain in Day 12.