integer to float conversion in python

Python – Convert Integer to Float

In this tutorial, we will look at how to convert an integer (int datatype) in Python to a float (float datatype) with the help of some examples.

How to convert int to float in Python?

integer to float conversion in python

You can use the Python built-in float() function to convert an integer to a float. Pass the integer you want to convert as an argument. The following is the syntax –

# convert integer i to float
float(i)

It returns the number as a float value.

Example

Let’s look at some examples of using the above syntax to convert an integer value to a float.

Here we apply the float() function on an integer value 12.

# integer variable
num = 12
# convert integer to float
num = float(num)
# display the number and its type
print(num)
print(type(num))

Output:

12.0
<class 'float'>

You can see that the variable num is of float type now with the value 12.0.

Alternatively, you can perform an arithmetic identity operation on the integer with a float value to get the resulting value as an integer. For example, multiply or divide the integer by 1.0 or add or subtract 0.0 to get the same value but as a float.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Here’s an example –

# integer variable
num = 12
# convert integer to float
print(num*1.0)
print(num/1.0)
print(num+0.0)
print(num-0.0)

Output:

12.0
12.0
12.0
12.0

You can see that all the above operations result in the float value 12.0

You might also be interested in –

  1. Extract Numbers From String in Python
  2. Python – Check if String Contains Only Numbers
  3. Python – Convert Integer to String


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top