Python float division

When we divide integer number in python the result show only the integer number, see this example:

a = 7
b = 2
c = a / b
print c
3
# the result will be 3, but it should be 3.5

To make right division with floating point we have to add this import line.

from __future__ import division
a = 7
b = 2
c = a / b
print c
3.5

print 10 / 3
3.33333333333

Another simple way to define python float division

print 10 / float(3)
3.33333333333

print float(10) / 3
3.33333333333


print 10 / 3.0
3.33333333333

print 10.0 / 3.0
3.33333333333

If this post was good and helpful for you, Please give it Like.
.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment