Sitemap

TYPE CONVERSION

3 min readJun 17, 2026

--

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:

  1. Implicit Type Conversion
  2. 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.0
result = x + y
print(result)

Output:

3.0

Explanation

  • x is an integer.
  • y is a float.
  • Python converts x into 1.0
  • The addition becomes:
1.0 + 2.0 = 3.0

The result is a float because converting float to int would lose the decimal information.

Example 2: Float and Complex

x = 1.0
y = 2 + 2j
result = x + y
print(result)

Output:

(3+2j)

Explanation

  • x is a float.
  • y is 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 → complex

2. 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.9
y = int(x)print(y)

Output:

10

Explanation

The decimal part .9 is removed and only the integer part remains.

2. float() Constructor

Converts data into a floating-point number.

Example

x = 10
y = float(x)print(y)

Output:

10.0

3. complex() Constructor

Converts data into a complex number.

Example

x = 10
y = 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.0

Float to Integer

num = 25.99
result = int(num)
print(result)

Output:

25

Integer 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" + 5

Output:

TypeError

Solution

Convert the string into an integer.

int("10") + 5

Output

15

Real-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(), and complex().

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.

--

--