Type conversion in python:
Type conversion is the process of converting the value from one data type to another data type. It means changing the type of the data.
Here x=10 is interger value but we converted that into string and float that are stored in y and z.
str(x) converts integer into a string and y stores that value similarly float(x) converts integer into a float and z stores that value.
Uses of type conversion:
adding two numbers is easy because both the values are integers but when we add different data types values it raises an error
thats why we need type conversion
here, we changed print(a+b) into print(a+int(b)) and it clears the error and shows the output in integer value.
Types:
Implicit type conversion and Explicit type conversion
Implicit type conversion:
It is the process in which python automatically converts one data type into another type when it required.
Python performs the automatic conversion in supported operations only like arithematic operations.
Get Sehwagbaipalli’s stories in your inbox
Join Medium for free to get updates from this writer.
it performs when the inputs are in certain numerical inputs like int, float and complex.
It does not automatically converts every data type , we observed in additon of int and string values shows error called TypeError.
Explicit Type Conversion:
It is the process of converting one data type into another data type manually by using built-in conversion functions.
User or programmer needs to convert the data type manually by their requirement.
int(), str(),float(),list(),bool() etc,.
user or programmer performs valid conversion by using conversion methods.
do not use invalid conversions
str(x) converts into integer by int(x) in first example because string value has numerical value but in second example string value has characters so it does not convert into integer values. so we do conversions in valid operations only not in all conditions like second example.






