In Python, this method is used to converts automatically one data type to another data type. This process doesn't need any user involvement.
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))
<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
integerwhile the data type of b is a
- Also, we can see the b has a
floatdata type because Python always converts smaller data types to larger data types to avoid the loss of data.