Topic : Type Conversion
DEFINITION :
Type conversion is a technique used to change or convert a data value from one data type to another data type.
For Example :
- Integer > Float
- String > Integer
- Integer > String
Type Conversion is divided into 2 types :
- IMPLICIT
- EXPLICIT
EXPLANATION :
- IMPLICIT :
Implicit type conversion is the process where python engine automatically converts one data type into another data type without the need of a programmer for execution.
“During implicit type conversion, Python engine follows the rules of type precedence to determine the resulting data type.”
The advantage of this data type is the data will be safe & it will never be vanished
Example :
when an integer and a float are used together, Python converts the integer to a float because float has higher type precedence in this context.
X=10 THIS IS AN INTEGER
Y=10.3 THIS IS AN FLOAT
Z=X+Y WHILE ADDING BOTH FLOAT HAS HIGH PRECEDENCE
SO INTEGER IS CONVERTED INTO FLOAT
OUTPUT: Z=20.3X=10.3
Y=5+2j
Z=X+Y
OUTPUT:
Z=15.3+2J2. EXPLICIT :
Get Sreedevarakondanavya’s stories in your inbox
Join Medium for free to get updates from this writer.
Explicit type conversion means the programmer manually converts a data value from one data type to another data type.
The disadvantage of using this data type is the data will not be safe, we might loss the data.
The programmer uses python inbuilt functions to convert the data type from one data type to another data.
Some of the inbuilt functions are :
- int( )
- float( )
- complex( )
- str( )
- list( )
In simple words:
Explicit type conversion= The programmer tells Python engine to change the data type
EXAMPLES :
- Integer > float
x=10
float(x)
OUTPUT: 10.02. Float > integer
x=10.3
int(x)
OUTPUT: 103. Float > complex
x=5.3
complex(x)
OUTPUT:5.3+1J4. Integer > float > complex
x=10
y=5.3
complex(x+y)
OUTPUT: 15.3+1J




