Before going to Data type conversion in Python procedure, you should have some basic knowledge of data types. Some of the most common data types in Python are numeric, string and boolean types. There are also built-in data types in Python that are dict, list, set and frozenset, and tuple.
What is data type conversion in Python?
As the name says, it is converting the value of one data type to another. This process is called data type conversion in Python.
How many types of conversion is there in Python?
Similar to most languages, there are two types of conversion.
- Implicit Type Conversion
- Explicit Type Conversion
What is Implicit Type Conversion?
In implicit conversion, Python converts one data type into another data type without any user involvement.
Let’s consider the following code.
int_number = 843 float_number = 8.43 result= int_number + float_number print("Data Type of Integer:",type(int_number)) print("Data Type of Float:",type(float_number)) print("Result Value:",result) print("Data Type of Result:",type(result))
('Data Type of Integer:', <type 'int'>) ('Data Type of Float:', <type 'float'>) ('Result Value:', 851.43) ('Data Type of Result:', <type 'float'>)
Note that there is no requirement for any conversion procedure here because Python will handle these kinds of conversions automatically.
What is Explicit Type Conversion?
In Explicit conversion, the user has to convert the data type of an object to the desired data type as per requirement. One can achieve the above using the default functions – float(), int(), string().
This process is sometimes called typecasting of data, as the user casts the data type of any object.
Consider the code below.
int_number = 843 string_number = “843” print("Data Type of Integer:",type(int_number)) print("Data Type of String:",type(string_number)) print(int_number + string_number)
Its output will throw the following error.
Data Type of Integer: <class 'int'> Data Type of String: <class 'str'> Traceback (most recent call last): File "<stdin>", line 6, in <module> print(int_number+string_number) TypeError: unsupported operand type(s) for +: 'int' and 'str'
In this scenario, Python cannot handle data type conversion automatically. So, we use Explicit conversion for these scenarios.
Check this code out!
int_number = 843 string_number = “843” print("Data Type of Integer:",type(int_number )) print("Data Type of String before casting:",type(string_number )) string_number = int(string_number ) print("Data Type of String after casting:",type(string_number )) result = int_number + string_number print("Result:",result ) print("Data type of result :",type(result ))
Data Type of Integer: <class 'int'> Data Type of String before casting: <class 'str'> Data Type of String after casting: <class 'int'> Result: 1686 Data type of result: <class 'int'>
In the above example, Python cannot convert data types automatically when you try to add two different data types that are not possible. To overcome such scenarios where Python cannot use implicit conversion, we use explicit conversion.
Here are some of the key points that you need to keep in mind when converting data types.
- Conversion always happens from one data type object to another data type object.
- Python interpreter will take care of Implicit conversion automatically, also, it avoids loss of data.
- Explicit Conversion also called Casting, when in-built functions were being used to perform this action.
- In Explicit Conversion, if we forcibly convert to a specific data type, there are chances for loss of data.
That’s all about conversion. If you have further questions or queries, write it in the comments.
Read more blogs related to Python such as 7 Reasons Why Python Beats PHP for Web Development and How to Hire a talented Python developer for your business. Get regular updates by subscribing to us.