Python Convert Int to Float

Convert Integer to Float in Python

To convert integer to float in python, you can use the float() class with the int passed as argument to it. Or use addition operator, with one of the operand as the given integer and the other as zero float; the result is a float of the same value as integer.

In this tutorial, we shall go through some of the ways to convert an integer to a float, with the help of example programs.

Example 1: Typecast Int to Float

In this example, we will take an integer, print it and its datatype, then convert it to float.

Python Program

a = 5
print(a,'is of type:',type(a))

a = float(a)
print(a,'is of type:',type(a))
Run this program

Output

5 is of type: <class 'int'>
5.0 is of type: <class 'float'>

Example 2: Typecast Int to Float (Another Way)

Or there is another way to do this. You can always add a 0 floating number to it and python implicitly promotes the type of the variable from int to float .

Python Program

a = 5
print(a,'is of type:',type(a))

a = a + 0.0
print(a,'is of type:',type(a))
Run this program

Output

5 is of type: <class 'int'>
5.0 is of type: <class 'float'>

Summary

In this tutorial of Python Examples, we converted/typecasted variable of int datatype to variable of float datatype, with the help of well detailed example programs.