Python Convert Int to Float

Convert Int to Float

To convert integer to float in Python, we can use the float() builtin function or implicit conversion.

In this tutorial, we shall go through these two methods of converting an integer to a float.

1. Convert int to float using float() built-in function

float() builtin function can take an integer as argument and return a floating-point number with the value as that of the given integer.

In the following program, we will take an integer, and convert it to float.

Python Program

# Take an integer
a = 5
print('Intput', a, type(a), sep='\n')

# Convert integer to float
output = float(a)
print('\nOutput', output, type(output), sep='\n')
Run Code Copy

Output

Intput
5
<class 'int'>

Output
5.0
<class 'float'>

2. Convert int to float using implicit conversion

Implicit conversion is the process where Python interpreter converts the given int to float when the int is added/subtracted/etc., with a floating-point number.

In the following program, we take an integer value, add it to a floating-point zero, and Python Interpreter takes care of the conversion.

Python Program

# Take an integer
a = 5
print('Intput', a, type(a), sep='\n')

# Convert integer to float implicitly
output = a + 0.0
print('\nOutput', output, type(output), sep='\n')
Run Code Copy

Output

Intput
5
<class 'int'>

Output
5.0
<class 'float'>

Summary

In this tutorial of Python Examples, we learned how to convert or implicitly typecast value of int datatype to that of float, with the help of well detailed example programs.

Related Tutorials

Code copied to clipboard successfully 👍