Facebook Pixel

Data Conversion

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

To convert an integer to a complex number, 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 the int() function. The int() function rounds off the given number to the nearest integer value.

To convert a float to a complex number, 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.