Convert the following integer type data into float type data implicitly and print it. a = 5
Python class 9
Answers
Solution:
The given code is written in Python.
a = 5
print('Value of a =', a)
a = a + 0.0
print('Now, the value of a =', a)
Explanation:
In any programming language, type conversion is the conversion of data type of a variable from one to other.
In Python, there are two types of casting:
- Implicit type casting.
- Explicit type casting.
Implicit type casting occurs when the compiler automatically converts the data type to other.
For example, in the above code, the initial value of a is 5 which is of type integer.
If we add float value to 5, the compiler will automatically convert the result to float. We add 0.0 (float value) to 5 so that its type is changed.
Explicit type conversion occurs when we want to forcibly convert one type to other which is not possible using implicit type casting.
For example, we can convert integer to float but not float to integer using implicit casting.
Example:
b = 5.0 - float
a = int(b) - a becomes 5 which is integer.
Refer to the attachment for output.