I want to write a function which inputs two variables a, b and which returns a/b as a float irrespective of the types of a and b. Right now I'm doing this as:
def f(a, b):
return float(a)/float(b)
Is there a better way to do this?
Assuming you are using Python2, put this at the top of the file
from __future__ import division
now / will always give a float. Use // for the old behaviour.
If you are using Python3, then this is already the default behaviour of /
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With