Implicit Type Conversion in Python
In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement. To get a more clear view of the topic see the below examples.
Example
As we can see the data type of ‘z’ got automatically changed to the “float” type while one variable x is of integer type while the other variable y is of float type. The reason for the float value not being converted into an integer instead is due to type promotion that allows performing operations by converting data into a wider-sized data type without any loss of information. This is a simple case of Implicit type conversion in Python.
Python3
x = 10 print ( "x is of type:" , type (x)) y = 10.6 print ( "y is of type:" , type (y)) z = x + y print (z) print ( "z is of type:" , type (z)) |
Output
x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
z is of type: <class 'float'>
Type Conversion in Python
Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions.
There are two types of Type Conversion in Python:
- Python Implicit Type Conversion
- Python Explicit Type Conversion
Contact Us