Q.Program 5-10: Program to show implicit conversion from int to float.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →When an int and a float meet in one expression, Python implicitly converts the int to float — 10 + 20.0 is 30.0, with no conversion function written by the programmer.
The idea (implicit type conversion / coercion). In implicit conversion the interpreter changes a value's type automatically, without any int()/float() call in the code. It happens in mixed-type arithmetic, and Python always converts toward the type that can hold both values without losing information — here float, since every int (like 10) has an exact float form (10.0), but a float like 20.5 has no int form without losing the fraction.
Program
# prog5-10.py
# Program to show implicit conversion from int to float
num1 = 10 # int
num2 = 20.0 # float
result = num1 + num2
print(result)
print(type(result))
print(type(num1)) # num1 itself is untouched
Expected output
30.0
<class 'float'>
<class 'int'>
Key lines
result = num1 + num2— before adding, Python silently converts10to10.0; the addition is then float + float, soresultis the float30.0.type(result)confirms<class 'float'>even though we never calledfloat()— that is what makes the conversion implicit. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.