Home >Backend Development >Python Tutorial >Why Does Python 3's `/` Operator Produce a Float Instead of an Integer?
In Python 2, integer division (i.e., /) resulted in an integer value. However, this behavior changed in Python 3. Consider:
>>> 2 / 2 1.0
Why does this division now yield a float instead of an integer?
The shift in division behavior is documented in PEP-238:
The // operator will be available to request floor division unambiguously.
This implies that integer division (/) now defaults to returning a float unless a // operator is explicitly used for floor division.
To obtain an integer result from division, you have two options:
>>> 2 // 2 1
>>> int(2 / 2) 1
In Python 3, integer division (/) returns a float by default. To obtain an integer result, you can either use the // operator for floor division or cast the result of / division to an integer.
The above is the detailed content of Why Does Python 3's `/` Operator Produce a Float Instead of an Integer?. For more information, please follow other related articles on the PHP Chinese website!