Data Conversion

 

Data Conversion

  • The process of converting numeric data from one type to another is called as type conversion.
  • To convert from integer to float, we use float() function.

 

To convert from integer to complex, we use the complex() function.


Example:

num1 = -25
num2 = float(num1)
num3 = complex(num1)

print(num2)
print(num3)

Output:

-25.0
(-25+0j)

 

  • To convert from float to integer, we use int() function. int() function rounds of the given number to the nearest integer value.

 

To convert from float to complex, we use the complex() function.

Example:

num1 = 8.4
num2 = int(num1)
num3 = complex(num1)

print(num2)
print(num3)

Output:

8
(8.4+0j)

 

Note: complex numbers cannot be converted to integer or float.