OneBite.Dev - Coding blog in a bite size

convert variable from int to float in python

Code snippet on how to convert variable from int to float in python

  my_int = 2
  my_float = float(my_int)

The code above converts an integer variable to a float variable in Python. The first line creates an integer variable called “my_int” and sets it to the value of 2. The second line creates a float variable called “my_float” and sets it to the value of the “my_int” variable, converted to a float. This is done by using the built-in float function which takes a single argument (the value of the “my_int” variable in this case) and returns the value as a float.

python