Python Type Conversion and Type Casting
Type Conversion:
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.
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion:
In Implicit type conversion, Python automatically converts one data type to another data type. This process doesn't need any user involvement.
Let's see an example where Python promotes conversion of lower datatype (integer) to higher data type (float) to avoid data loss.
When we run the above program, the output will be
datatype of num_int: <class 'int'>
datatype of num_flo: <class 'float'>
Value of num_new: 124.23
datatype of num_new: <class 'float'>
In the above program,
- We add two variables num_int and num_flo, storing the value in num_new.
- We will look at the data type of all three objects respectively.
- In the output we can see the datatype of num_int is an
integer
, datatype of num_flo is afloat
. - Also, we can see the num_new has
float
data type because Python always converts smaller data type to larger data type to avoid the loss of data.Now, let's try adding a string and an integer, and see how Python treats it.
When we run the above program, the output will be
Data type of num_int: <class 'int'>
Data type of num_str: <class 'str'>
Traceback (most recent call last):
File "python", line 7, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In the above program,
- We add two variable num_int and num_str.
- As we can see from the output, we got
typeerror
. Python is not able use Implicit Conversion in such condition. - However Python has the solution for this type of situation which is know as Explicit Conversion
Explicit Type Conversion:
In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like
int()
, float()
, str()
, etc to perform explicit type conversion.
This type conversion is also called typecasting because the user casts (change) the data type of the objects.
Syntax :
(required_datatype)(expression)
Typecasting can be done by assigning the required data type function to the expression.
Example 3: Addition of string and integer using explicit conversion
When we run the above program, the output will be
Data type of num_int: <class 'int'>
Data type of num_str before Type Casting: <class 'str'>
Data type of num_str after Type Casting: <class 'int'>
Sum of num_int and num_str: 579
Data type of the sum: <class 'int'>
In above program,
- We add num_str and num_int variable.
- We converted num_str from string(higher) to integer(lower) type using
int()
function to perform the addition. - After converting num_str to a integer value Python is able to add these two variable.
- We got the num_sum value and data type to be integer.
Key Points to Remember:
- 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 object are converted using predefined function by user.
- In Type Casting loss of data may occur as we enforce the object to specific data type.
Good post!Thank you so much for sharing this pretty post,it was so good to read and useful to improve my knowledge as updated one,keep blogging.
ReplyDeletePython training in Electronic City