English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Before learning about type conversion in Python, you should understand Python Data Types.
The process of converting the value of one data type (integer, string, floating-point number, etc.) to another data type is called type conversion. Python has two types of type conversions.
Implicit Type Conversion
Explicit Type Conversion
In implicit type conversion, Python automatically converts one data type to another. This process does not require any user involvement.
Let's look at an example where Python promotes the conversion of lower data types (integers) to higher data types (floating-point numbers) to avoid data loss.
num_int = 123 num_flo = 1.23 num_new = num_int + num_flo print("num_int data type:", type(num_int)) print("The data type of num_flo:", type(num_flo)) print("The value of num_new:", num_new) print("The data type of num_new:", type(num_new))
When we run the above program, the output will be:
num_int data type: <class 'int'> The data type of num_flo: <class 'float'> The value of num_new: 124.23 The data type of num_new: <class 'float'>
in the above program
We add two variablesnum_intandnum_flo additionand store the value in the variablein num_new.
we check the data types of all three objects separately.
In the output, we can seenum_intwhose data type is integer,num_flowhose data type is a float.
Additionally, we can seenum_newwith float data type because Python always converts the smaller data type to the larger data type to avoid data loss.
Now, let's try to add a string and an integer and see how Python handles it.
num_int = 123 num_str = "456" print("num_int data type:", type(num_int)) print("The data type of num_str:", type(num_str)) print(num_int+num_str)
When we run the above program, the output will be:
num_int data type: <class 'int'> The 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 have two variablesnum_int andnum_straddition.
From the output, we can see that we got a TypeError. In this case, Python cannot use implicit conversion.
However, Python provides a solution for such cases called "Explicit Conversion".
In "Explicit Type ConversionIn it, the user converts the data type of the object to the required data type. We use explicit type conversions with predefined functions like int(), float(), str(), and so on.
This type of conversion is also called type conversion because the user forces the conversion (change) of the object's data type.
Syntax :
<required_datatype>(expression)
Type conversion can be completed by assigning the required data type function to the expression.
num_int = 123 num_str = "456" print("num_int data type:", type(num_int)) print("The data type of num_str before conversion:", type(num_str)) num_str = int(num_str) print("The data type of the converted num_str:", type(num_str)) num_sum = num_int + num_str print("num_int and num_str sum:", num_sum) print("sum data type:", type(num_sum))
When we run the above program, the output will be:
num_int data type: <class 'int'> The data type of num_str before conversion: <class 'str'> The data type of the converted num_str: <class 'int'> num_int and num_str sum: 579 sum data type: <class 'int'>
in the above program
Wenum_strandnum_intadd variables.
We use the int() function tonum_strfrom string (high) to integer (low) type to perform addition.
Convertnum_strAfter converting to integer, Python can perform addition on these two variables.
Finally, we getnum_sumValue and and the data type of the value is integer.
Type conversion is the conversion of an object from one data type to another.
Implicit type conversion is automatically executed by the Python interpreter.
Python avoids data loss in implicit type conversion.
Explicit type conversion is also known as type conversion, where users use predefined functions to convert the data type of an object.
In type conversion, when we force an object to a specific data type, data loss may occur.