Forceful Type Casting
In the last lesson Python changed a type on its own, and only ever between numbers. Now you take charge: when Python will not convert for you — turning text into a number, above all — you command the conversion yourself. That is forceful, or explicit, casting — also called explicit type conversion.
1The problem automatic casting will not solve
You met this wall with input(). Whatever the user types comes back as text, and Python refuses to guess that the text was meant as a number. Add two such values and they join end to end instead of adding up:
a = input() # user types 10
b = input() # user types 5
print(a + b) # '10' + '5'105
Automatic casting cannot help here — it never mixes text with numbers. To get 15, you have to force the text into a number yourself. Python gives you a small set of functions for exactly that.
2The four casting functions
Each function takes a value and builds a new value of its own type. Pick a value below and try each one — watch which conversions are clean, which lose something, and which Python refuses outright:
int() does not round, it cuts. That is why int(9.8) is 9, not 10.Makes a whole number. From a float it cuts off the decimals — it does not round.
Makes a decimal number. A whole number gains a .0.
Makes text. Anything at all can become a string.
Makes True or False. Zero and empty become False; everything else True.
3The one you will use most: int(input())
Wrapping input() in int() or float() is how nearly every program takes a number from the user. The two calls sit on one line, and the trick is the order Python runs them: inside-out. The inner input() runs first and produces text, then the outer int() forces that text into a number. Take it one step at a time with Next step, or watch the whole thing with Play all:
Two calls on one line. Python starts with the inner one, input().
So age = int(input()) is really three steps in one line: input() gives the text '25', int('25') gives the number 25, and = stores it. Here is the adding program, fixed at last — run it and type a number each time it stops:
4Two things to watch for
int() cuts, it does not round. Converting a float to an int throws the decimal part away — it does not round to the nearest whole number. If you want rounding, that is a different tool (round()):
The text has to spell a number. int() can convert '50', but not 'fifty' and not even '9.8' (that has a decimal point, so it is not a whole number). When it cannot, Python raises a ValueError rather than guess:
print(int('hello'))ValueError: invalid literal for int() with base 10: 'hello'
name = input() needs no casting at all. Reach for int() or float() only when you actually need to do maths with the value. Converting for no reason just adds a place for a ValueError to appear.5Recap
int(), float(), str() or bool(). Unlike automatic casting, it can turn text into a number — which is why int(input()) and float(input()) are everywhere. Python runs the inner call first. int() cuts decimals off rather than rounding, and raises a ValueError if the text is not really a number.Why do we write int(input()) instead of just input() when we want a number?
What is int(9.8) ?
In age = int(input()), which runs first?
What does int('9.8') do?