I am facing the problem while dividing
my max_sum = 14
total_no=4
so when i do
print "x :", (total_sum/total_no)
, I get 3 and not 3.5
I tried many ways for printing but failed, can somebody let me know what way I get in 3.5 format?
Thank you
In Python 2.x, dividing two integers by default dives you another integer. This is often confusing, and has been fixed in Python 3.x. You can bypass it by casting one of the numbers to a float, which will automatically cast the other:
float( 14 ) / 4 == 3.5The relevant PEP is number 238:
It was not changed in Python 2.x because of severe backwards-compatibility issues, but was one of the major changes in Python 3.x. You can force the new division with the line
at the top of your Python script. This is a
__future__-import — it is used to force syntax changes that otherwise might break your script. There are many other__future__imports; they are often a good idea to use in preparation for a move to Python 3.x.Note that the
//operator always means integer division; if you really want this behaviour, you should use it in preference to/. Remember, “explicit is better than implicit”!