Automatic Type Casting
Changing a value from one type to another is called type casting — or type conversion. The two names mean exactly the same thing. Sometimes you ask for it, and sometimes Python does it for you without a word. This lesson is about the quiet kind — the conversion that happens on its own.
1Python's rule: both sides must be the same type
Python has a quiet rule about arithmetic. An operator like + can only combine two operands when they are of the same type. Two ints, fine. Two floats, fine. But what about 2 + 3.5, where one side is an int and the other a float? By the rule, Python cannot add them as they stand — they do not match.
print(2 + 3.5)5.5
And yet it works, and gives 5.5. Here is what Python does behind the scenes: it converts the int 2 into the float 2.0, so that now both sides are floats. Then, obeying its own rule, it adds float + float and gets the float 5.5. That silent conversion is automatic type casting, also called implicit casting — you never asked for it.
When the two sides already match, there is nothing to convert. In 2.5 + 3.5 both operands are floats already, so Python adds them straight away — float + float → float — and gives 6.0.
2 into 2.0, or turn 3.5 into 3. It always picks the first, because turning 3.5 into 3 would throw away the .5. Widening a value loses nothing; narrowing it does. So the narrower value is always the one that changes.2Watch the conversion happen
Below, the same-type rule plays out one beat at a time. Python spots that the two sides do not match, casts the narrower one up so they do, and only then combines them. Pick an expression, then take it a step at a time with Next step or watch the whole thing with Play all — the moment one operand changes type is the casting itself:
Python reads 2 + 3.5.
The types line up on a ladder from narrowest to widest — bool → int → float → complex — and the lower one always climbs to meet the higher. bool sits right at the bottom: in Python True counts as 1 and False as 0, so a bool can be cast up like any other number:
3The one you meet every day: division
You have already seen automatic casting without knowing its name. The ordinary division operator / always gives a float, even when it divides evenly and even when both numbers are ints:
10 / 2 is 5.0, not 5. Python widens the answer to a float so that division behaves the same way every time, whether or not it comes out even.
4Where automatic casting stops
Automatic casting only ever happens between numbers. Python will never quietly turn text into a number for you. So while 10 + 2.5 works smoothly, mixing a string with a number is simply an error:
print('10' + 5)TypeError: can only concatenate str (not "int") to str
This is the very problem you met with input(): the value comes back as text, and Python will not guess that you meant a number. Turning text into a number is a job you have to ask for by name — and that is the next lesson.
bool → int → float → complex. It happens only between number types. It never turns text into a number.What is the type of the result of 4 + 1.0 ?
What does 10 / 2 give in Python?
Why does Python turn 3 into 3.0 in 3 + 0.5, instead of turning 0.5 into 0?
What is the result of print('10' + 5) ?