Python Type Conversion
Today, I learned about Type Conversion in Python.
Type conversion is the process of converting a value from one data type to another data type.
For example, we may convert:
int→floatfloat→intint→strstr→int- and many other combinations.
In Python, type conversion mainly happens in two ways:
- Implicit Type Conversion
- Explicit Type Conversion
1. Implicit Type Conversion
Implicit Type Conversion means that Python automatically converts one data type into another when it is necessary.
In simple words, we don’t have to tell Python to perform the conversion. The Python interpreter does it automatically.
Let’s understand this with an example.
Example
# Type conversion 1 - IMPLICITx = 12
y = 12.7
z = 14print(type(x + y + z))
Output
<class 'float'>Here:
x = 12x is an int.
y = 12.7y is a float.
z = 14z is also an int.
When we perform:
x + y + zPython needs to perform the calculation using compatible numeric types. Because a float is involved, the integer values are promoted to float for the calculation.
Therefore, the final result is a float.
Simple Flow
Integer + Float → Python automatically converts/promotes the integer → Calculation → Float result
[IMAGE PLACEHOLDER — Insert the “Python Implicit Type Conversion” flowchart here]
Download the Python Implicit Type Conversion flowchart
2. Explicit Type Conversion
Explicit Type Conversion means that we manually convert one data type into another.
In simple words, we tell Python which data type we want the value to be converted into.
For this, Python provides built-in functions such as:
int()
float()
str()
bool()
complex()Let’s understand this with an example.
Example
# Type conversion 2 - EXPLICITx = 10.3435463
y = 452342print(int(x))
print(type(x))print(float(y))
print(type(y))
Output
10
<class 'float'>
452342.0
<class 'int'>Here:
x = 10.3435463The value of x is a float.
When we use:
int(x)Python converts the value from float to int.
The result is:
10Notice that the decimal part is removed. int() does not round the number; it removes the fractional part.
Similarly:
y = 452342y is an int.
Get Nikesh Varma Kodali’s stories in your inbox
Join Medium for free to get updates from this writer.
When we use:
float(y)Python converts the integer into a floating-point number:
452342.0Simple Flow
Float → int() → Integer
Integer → float() → Float
[IMAGE PLACEHOLDER — Insert the “Python Explicit Type Conversion” flowchart here]
Download the Python Explicit Type Conversion flowchart
Some Common Type Conversion Functions
Python provides several built-in functions for explicit type conversion.
Function — — — — — — — -> Converts a value to
int() ----------------> Integer
float()---------------->Floating-point number
str()---------------->String
bool()---------------->Boolean
complex()---------------->Complex number
x = "100"print(int(x))
print(type(int(x)))Output:
100
<class 'int'>Here, the string "100" is explicitly converted into an integer using int().
A Small Example with Different Data Types
Let’s also look at a complex number.
x = 24 + 6jprint(type(x))Output
<class 'complex'>Here, x is a complex number, not a float.
A complex number in Python contains a real part and an imaginary part.
In this example:
24 + 6j24→ Real part6j→ Imaginary part
So Python identifies the value as:
complexThis is an important point because the screenshot you originally provided showed:
<class 'float'>for the complex-number example. That output would be incorrect for:
x = 24 + 6j
print(type(x))The correct output is:
<class 'complex'>Implicit vs Explicit Type Conversion
Now let’s quickly understand the difference.
Implicit Conversion
Python performs the conversion automatically.
x = 10
y = 2.5result = x + yprint(result)
print(type(result))
Output:
12.5
<class 'float'>We didn’t manually convert x. Python handled the type promotion during the operation.
Explicit Conversion
We perform the conversion manually using a function.
x = 10.5y = int(x)print(y)
print(type(y))
Output:
10
<class 'int'>Here, we explicitly told Python to convert x into an integer.


