Course outline · 0% complete

0/32 lessons0%

Course overview →

int, float, and Conversion

lesson 3-1 · ~10 min · 7/32

Recall from lesson 1-1 how values are sorted into types. The type of "3.5", quotes included, is str.

The quotes are decisive even though the contents look numeric. "3.5" is three characters of text, a digit, a dot, and another digit, and Python treats it as text until told otherwise. Arithmetic on it is not possible in that form, and converting it into a number is exactly what this lesson covers.

Converting between types

Every value that enters a program from the outside, typed input, a file, a web request, arrives as text. Until you convert it, you cannot do math with it. Conversion functions are the doorway between the outside world and arithmetic, which is why this small lesson unlocks everything interactive that follows.

Python never guesses a conversion for you. "3" + 3 is an error on purpose. When you have a value of the wrong type, convert it explicitly:

FunctionConverts toExample
int(x)whole numberint("42")42
float(x)decimalfloat("3.5")3.5
str(x)textstr(42)"42"

Two behaviors to know precisely:

  • int(9.9) gives 9. Converting a float to int chops off the decimal part, it does not round.
  • round(x) rounds to the nearest whole number, and round(x, 2) rounds to 2 decimal places.

If the text cannot be converted, like int("hello"), the program stops with a ValueError. Unit 9 shows how to survive that.

int(9.9) truncates 9 10 9.9 back to 9 it drops the decimal part, whatever its size round(9.9) goes to the nearest 9 10 9.9 forward to 10 Same input, different answers, so the choice is a decision about meaning: int for how many WHOLE units fit, round for the closest value. round(x, 2) keeps two decimals instead of going to a whole number.
Why int and round disagree on the same number. On the left, int applied to 9.9 discards the decimal part outright and lands back on 9, no matter how close to 10 the value was. On the right, round moves to whichever whole number is nearest and so lands on 10. Choosing between them is a decision about meaning, int for counting whole units that fit and round for the closest value.

Conversions and rounding compared

Each line below applies one conversion from the table. The pair worth studying is int(9.9) against round(9.9), since they answer the same question differently.

print(int("42") + 1)
print(float("3.5") * 2)
print(int(9.9))
print(round(9.9))
print(round(3.14159, 2))
print(str(42) + "!")

Output

43
7.0
9
10
3.14
42!

The first line converts before adding, which is why it produces the number 43 rather than joining any text. The second produces 7.0 because a float in a calculation keeps the result a float. Then int(9.9) gives 9 while round(9.9) gives 10, the difference being that int discards the decimal part outright and round moves to the nearest whole number. The last line runs the conversion in the other direction, turning 42 into text so that + joins it to "!".

int(7.8) evaluates to 7.

Converting a float with int truncates it, discarding everything after the decimal point regardless of how large that part is. Even int(7.999) gives 7. Nearest-number behavior requires round instead, and round(7.8) gives 8. Choosing between them matters whenever the value represents a measurement rather than a count of completed units.

Prices arriving as text need converting before any arithmetic can touch them. This snippet converts, applies a 10 percent discount, and rounds the result for display.

raw = "25.00"
price = float(raw)
discounted = price * 0.9
print(round(discounted, 2))

Output

22.5

float(raw) is what makes the multiplication legal, since "25.00" * 0.9 would fail. Multiplying by 0.9 keeps 90 percent of the price, which is the discount stated as what remains rather than what is removed. The printed 22.5 shows something about round: asked for two decimals it does not pad with a trailing zero, because it returns a number rather than formatted text. Producing 22.50 on screen would call for an f-string with :.2f.

float("7") + 1 evaluates to 8.0, printed with the decimal point.

Two steps produce that result. The conversion turns the text "7" into the float 7.0, and adding the int 1 to a float yields a float. Python widens the result to the more general type rather than dropping precision, so once a float takes part in a calculation the answer stays a float. That is the reason for the trailing .0, and it is the same rule that made 10 / 5 print as 2.0 back in unit 1.