TYPE CONVERSION
Introduction
In Python, different kinds of data are stored using different data types such as integers, floating-point numbers, strings, and complex numbers. Sometimes, while performing operations, we need to change data from one type to another. This process is called Type Conversion.
Definition:
Type Conversion is the technique of converting data from one data type to another data type.
Python supports two types of type conversion:
- Implicit Type Conversion
- Explicit Type Conversion
1. Implicit Type Conversion
Implicit Type Conversion is performed automatically by the Python Interpreter (Python Engine). The programmer does not need to write any extra code.
Python only performs implicit conversion when there is no loss of information.
How Does It Work?
Python follows a priority (precedence) order among numeric data types:
int → float → complex
Higher-priority data types can safely store values of lower-priority data types.
Example 1: Integer and Float
x = 1
y = 2.0result = x + y
print(result)Output:
3.0Explanation
xis an integer.yis a float.- Python converts
xinto1.0 - The addition becomes:
1.0 + 2.0 = 3.0The result is a float because converting float to int would lose the decimal information.
Example 2: Float and Complex
x = 1.0
y = 2 + 2jresult = x + y
print(result)Output:
(3+2j)Explanation
xis a float.yis a complex number.- Python converts the float into a complex number automatically.
- The operation becomes:
(1+0j) + (2+2j)Result:
(3+2j)Since converting a complex number into a float would lose the imaginary part, Python converts the float to complex instead.
Key Points of Implicit Conversion
✅ Done automatically by Python
✅ No code required from the programmer
✅ No loss of data
✅ Follows precedence:
int → float → complex2. Explicit Type Conversion
Explicit Type Conversion is performed manually by the programmer.
In this method, the programmer decides which data type should be converted into another type using constructors.
Why Explicit Conversion?
Sometimes Python cannot automatically convert data because information may be lost.
Get Burra Chandana’s stories in your inbox
Join Medium for free to get updates from this writer.
In such cases, we use explicit conversion.
Syntax
datatype(value)Example:
int(10.5)
float(10)
complex(5)Constructors Used for Explicit Conversion
1. int() Constructor
Converts data into an integer.
Example
x = 10.9y = int(x)print(y)
Output:
10Explanation
The decimal part .9 is removed and only the integer part remains.
2. float() Constructor
Converts data into a floating-point number.
Example
x = 10y = float(x)print(y)
Output:
10.03. complex() Constructor
Converts data into a complex number.
Example
x = 10y = complex(x)print(y)
Output:
(10+0j)Complex constructor can convert:
- Integer → Complex
- Float → Complex
- Complex → Complex
Practical Examples
Integer to Float
num = 25
result = float(num)print(result)Output:25.0Float to Integer
num = 25.99
result = int(num)print(result)Output:
25Integer to Complex
num = 7
result = complex(num)print(result)Output:
(7+0j)Common Errors During Type Conversion
TypeError
Occurs when Python cannot perform an operation because of incompatible data types.
Example:
"10" + 5Output:
TypeErrorSolution
Convert the string into an integer.
int("10") + 5Output
15Real-Life Example
Imagine you are filling an online form.
- Age entered as
"21"→ String - To calculate eligibility, the system converts it to
21→ Integer
This conversion helps the program perform mathematical operations correctly.
Advantages of Type Conversion
- Improves data compatibility.
- Helps perform mathematical operations.
- Makes programs more flexible.
- Reduces datatype-related errors.
- Enables interaction between different data types.
Conclusion
Type Conversion is one of the most important concepts in Python programming. It allows data to move from one datatype to another whenever required. Python provides two methods:
- Implicit Type Conversion — done automatically by Python without losing information.
- Explicit Type Conversion — done manually by the programmer using constructors such as
int(),float(), andcomplex().
Understanding type conversion helps programmers write efficient, error-free, and flexible Python programs. As you continue learning Python, mastering this concept will make handling data much easier and more powerful.





