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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you are using Python2, put this at the top of the file
now
/will always give a float. Use//for the old behaviour.If you are using Python3, then this is already the default behaviour of
/