In Python, this method is used to converts automatically one data type to another data type. This process doesn't need any user involvement.
Example:
a = 7
b = 3.0
print(type(a))
print(type(b))
c = a + b
print('c =',c)
print(type(c))
d = a * b
print('d =',d)
print(type(d))
Output:
<class 'int'> <class 'float'> c = 10.0 <class 'float'> d = 21.0 <class 'float'>
In the above Example,
- We will look at the data type of all four objects respectively.
- In the output, we can see the data type of a is an
integer
while the data type of b is afloat
. - Also, we can see the b has a
float
data type because Python always converts smaller data types to larger data types to avoid the loss of data.
Tags