Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 integer division [duplicate]

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?

like image 923
Megatron Avatar asked Oct 22 '13 02:10

Megatron


People also ask

Does Python 3 do integer division?

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.

Does division in Python return integer?

In Python 2.7: By default, division operator will return integer output.

How does Python handle integer division?

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.

Does division in Python always float?

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.


1 Answers

Try this:

a = 1
b = 2
int_div  = a // b
like image 118
Lucas Ribeiro Avatar answered Sep 28 '22 21:09

Lucas Ribeiro