In this article, you will learn about the Type conversion and uses of type conversion.
Type Casting is the method to convert the variable data type into a certain data type in order to the operation required to be performed by users.
Casting in python is therefore done using constructor functions:
Examples for Integers
a=int (634 ) #Output is 634
b=int (6.73 ) #Output is 6
Examples for Float Numbers
a=float (375 ) #Output is 375.0
b=float (3.776 ) #Output is 3.776
Examples for Strings
a=str ("this is rguktweb team" ) #Output is 'this is rgukt web team'
b=str (3.776 ) #Output is '3.776'
The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
Python has two types of type conversion.
In Implicit type conversion, Python automatically converts one data type to another data type.
This process doesn't need any user involvement.
In this, methods, Python converts data type into another data type automatically.
In this process, users don’t have to involve in this process.
Example to convert integer to float
int_num=143
float_num=1.23
new_num= int_num+float_num
print ("data type of int_num is " ,type (int_num))
#Output is datatype of int_num: <class 'int'>
print ("data type of float_num is " ,type (float_num))
#Output is datatype of float_num: <class 'float'>
print ("data type of new_num is " ,type (new_num))
#output is datatype of new_num: <class 'float'>
In the above program,
int_num=123
str_num="143"
print ("Data type of int_num:" ,type (int_num))
#Output is Data type of int_num: <class 'int'>
print ("Data type of str_num:" ,type (str_num))
#Output is Data type of str_num: <class 'str'>
print (int_num+str_num)
Traceback (most recent call last):
File "python", line 7, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
in the above programm
In Explicit Type Conversion, users convert the data type of an object to required data type.
We use the predefined functions like
This type of conversion is also called typecasting because the user casts (changes) the data type of the objects.
Example for Addition of string and integer using explicit conversion
A=143
B="253"
print ("Data type of A:" ,type (A))
#Output is Data type of A: <class 'int'>
print ("Data type of B before Type Casting:" ,type (B))
#Output is Data type of B before Type Casting: <class 'str'>
B=int (B)
print ("Data type of B after Type Casting:" ,type (B))
#Output is Data type of B after Type Casting: <class 'int'>
C=A+B
print ("Sum of A and B:" ,C)
#Output is Sum of A and B:396
print ("Data type of the sum:" ,type (C))
#Output is Data type of the sum: <class 'int'>
In the above program
Type Conversion is the conversion of object from one data type to another data type.
Implicit Type Conversion is automatically performed by the Python interpreter.
Python avoids the loss of data in Implicit Type Conversion.
Explicit Type Conversion is also called Type Casting, the data types of objects are converted using predefined functions by the user.
In Type Casting, loss of data may occur as we enforce the object to a specific data type.