In Python 3 vs Python 2.6, I've noticed that I can divide two integers and get a float. How do you get the Python 2.6 behaviour back?
Is there a different method to get int/int = int?
In Python 3. x, slash operator ("/") does true division for all types including integers, and therefore, e.g. 3/2==1.5. The result is of a floating-point type even if both inputs are integers: 4 / 2 yields 2.0.
In Python 2.7: By default, division operator will return integer output.
Integer division is the division of one integer by another in which the resulting number is truncated (ie. decimal places are dropped), such that the quotient is also an integer. This is the default behavior in Python 2.7, but not Python 3. For example, 3 / 2 returns 1 in Python 2.7, but 1.5 in Python 3.
Adding subtracting or multiplying two ints always yields an int result, but division is different. The result of division is always a float value, even if the division comes out even.
Try this:
a = 1
b = 2
int_div = a // b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With