Python Coding Exercise #31: Typecasting
Part of the series Python Practice Exercise — part 31
Before you start: Know input() and float() · Know f-strings
The question
When programming in Python, often we need to change the data type of a value. For example, a floating point number can be converted into an integer with the function
int:
temperature = float(input("Please type in a temperature: "))
print("The temperature is", temperature)
print("...and rounded down it is", int(temperature))
Please type in a temperature: 5.15
The temperature is 5.15
...and rounded down it is 5
Notice the function always rounds down, and not according to the rounding rules in mathematics. This is an example of a floor function.
Please type in a temperature: 8.99
The temperature is 8.99
...and rounded down it is 8
Please write a program which asks the user for a floating point number and then prints out the integer part and the decimal part separately. Use the Python
intfunction. You can assume the number given by the user is always greater than zero.
An example of expected behaviour:
Please type in a number: 1.34
Integer part: 1
Decimal part: 0.34
My Solution
number = float(input("Please type in a number: "))
integer_part = int(number)
print(f"Integer part: {integer_part}")
print(f"Decimal part: {number-integer_part}")
What is typecasting?
Typecasting (also called type conversion) means changing a value from one data type to another. Python has a conversion function named after each type:
| Function | What it does | Example | Result |
|---|---|---|---|
int() | converts to a whole number | int(5.15) | 5 |
float() | converts to a decimal number | float("5.15") | 5.15 |
str() | converts to text | str(5) | "5" |
You have been typecasting since the first int(input(...)). The input() function returns text, and int() or float() converts it to a number. (More in Data Types in Python.)
int() cuts off, it doesn’t round
When int() converts a decimal number, it simply drops everything after the decimal point. It doesn’t round to the nearest whole number:
print(int(5.15)) # 5
print(int(8.99)) # 8, not 9
print(round(8.99)) # 9
That’s what the exercise means by “rounds down”. For a number greater than zero, int() always gives the whole number just below it. The exercise tells you to assume the number is greater than zero.
One more point: int() can’t convert text that contains a decimal point.
print(int(float("5.15"))) # 5, converting in two steps works
print(int("5.15")) # error
ValueError: invalid literal for int() with base 10: '5.15'
That’s why My Solution reads the input with float() first. A text such as "5.15" has to become a float before it can become an integer.
How My Solution Works
1. Read the number as a float. float(input(...)) converts the typed text to a decimal number, such as 1.34.
2. Get the integer part. int(number) cuts off the decimal part, so 1.34 becomes 1. This is stored in integer_part.
3. Get the decimal part by subtracting. If you take the whole-number part away from the original number, what’s left is the decimal part: 1.34 - 1. The program calculates this directly inside the f-string, with {number-integer_part}.
4. Print both parts. Two f-strings print the integer part and the decimal part on separate lines.
University Solution
The University Solution is the same program as mine, with the subtraction stored in a variable first:
number = float(input("Please type in a number: "))
intpart = int(number)
decpart = number - intpart
print(f"Integer part: {intpart}")
print(f"Decimal part: {decpart}")
Giving the calculation its own line and name, decpart, makes the final print() easier to read. The output is exactly the same. (This is the same idea as storing age in its own variable in Exercise #08.)
The decimal part doesn’t print as 0.34
The sample output in the exercise shows Decimal part: 0.34. But if you run the program with 1.34, Python actually prints:
Please type in a number: 1.34
Integer part: 1
Decimal part: 0.3400000000000001
This isn’t a mistake in the program. Computers store decimal numbers in binary, and many decimals, such as 1.34, can’t be stored exactly. So subtracting gives a number that is extremely close to 0.34, but not exactly equal. (The same effect appeared as 0.1 + 0.2 giving 0.30000000000000004 in Data Types in Python.) Binary and other number systems are explained in Exercise #24.
Here is what the program really prints for some other numbers:
| Input | Integer part | Decimal part printed |
|---|---|---|
| 1.34 | 1 | 0.3400000000000001 |
| 5.15 | 5 | 0.15000000000000036 |
| 8.99 | 8 | 0.9900000000000002 |
| 2.5 | 2 | 0.5 |
| 3.0 | 3 | 0.0 |
Numbers that can be stored exactly, such as 2.5 and 0.75, come out clean. Most others don’t. The University Solution doesn’t round either, so it prints the same long digits.
Should you round it?
It’s tempting to tidy the output with round(number - integer_part, 2). That gives a clean 0.34 for 1.34. But rounding changes the answer, and it can lose information. For 3.14159, the real decimal part is 0.14159, and rounding to two places turns it into 0.14:
| Input | Without rounding | With round(..., 2) |
|---|---|---|
| 1.34 | 0.3400000000000001 | 0.34 |
| 3.14159 | 0.14158999999999988 | 0.14 |
So rounding is a good choice only when you know how many decimal places you want. For this exercise, the program doesn’t know how many decimals the user will type, so it’s safest to print the calculated value as it is. (If you want to see how rounding can also make an exact-output check fail, see Exercise #19.)
Key takeaways
- Typecasting means converting a value from one type to another, with
int(),float()andstr(). int()drops the decimal part. It doesn’t round:int(8.99)is8.int("5.15")fails. Convert text to a float first, and then to an integer if you need to.- The decimal part of a number is the number minus its integer part.
- Decimal numbers are stored in binary, so subtraction can leave tiny extra digits, such as
0.3400000000000001. - Rounding makes the output tidier, but it can also change or lose digits. Round only when you know how many decimal places you need.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.