Printing floating point number in python
How can I print the following lines of code but get a floating point number of of it instead of a rounded int?
print "Math Question: ",开发者_JAVA百科 100 - 25 * 3 % 4
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
Division with /
in 2.x results in a integer if both operands are integers. Either cast one of the operands to a float first, or from __future__ import division
.
from __future__ import divison
or run Python with the -Qnew
option
Python3 has this behaviour by default
If you use integers, Python assumes you really mean them to be integers and does integer math. If you use floats, it will do floating point math.
Try something like this to get floating point output:
print 3/4.0
精彩评论