By: Archana Shukla and Rajesh Shukla
We can convert from one data type to another. Such conversions can happen in two ways. Explicit (forced) and implicit.
This type of conversions happen when the user or the programmer forces it in the program. there is a risk of data loss in this type of conversion as we are forcing the expression to be of a specific type.
x = 3 # int y = 4.5 # float z = 5j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x) print(a) print(b) print(c) print(type(a)) print(type(b)) print(type(c))
These are also known as coercion. This conversions are done automatically by the python interpreter on the basis of requirement.
#implicit conversion from int to float n1=10 n2=55.0 sum1=n1+n2 print(sum1) print(type(sum1))