I thought 0.5
would come out if I said 1/2
, but 0
came out
If you divide two integers, you just cut off the decimal point
When a and b are int
c = a/b
How do we get c to float as a result?
a = 1
b = 2
print a/b
division import
modules.
from__future__import division #Not required for Python 3 or higher
print 1/2
If you do not write __future___
, you can write as follows.
However, this method can cause TypeError
, so use it carefully.
a = 1
b = 2
float(a)/float(b)
You can use it with this
* Python 3 saves the result of int
/int
as float
. So you don't have to import anything, and you don't have to change the type.
© 2025 OneMinuteCode. All rights reserved.