By Jonas Byström


2009-08-15 21:48:39 8 Comments

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0

Is this intended? I strongly remember earlier versions returning int/int=int? What should I do, is there a new division operator or must I always cast?

4 comments

@VijayNaidu 2017-07-02 06:57:36

Hope it might help someone instantly.

Behavior of Division Operator in Python 2.7 and Python 3

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

to get the result in double multiple 1.0 to "dividend or divisor"

100/35 => 2 #(Expected is 2.857142857142857)
(100*1.0)/35 => 2.857142857142857
100/(35*1.0) => 2.857142857142857

In Python 3

// => used for integer output
/ => used for double output

100/35 => 2.857142857142857
100//35 => 2
100.//35 => 2.0    # floating-point result if divsor or dividend real

@Tal J. Levy 2018-10-08 09:26:27

By the way, no need to multiply by 1.0. It is enough that one of the numbers is a float. E.g., 100/35.0 = 100.0/35 = 2.857142857142857

@code_onkel 2016-09-05 14:18:11

The accepted answer already mentions PEP 238. I just want to add a quick look behind the scenes for those interested in what's going on without reading the whole PEP.

Python maps operators like +, -, * and / to special functions, such that e.g. a + b is equivalent to

a.__add__(b)

Regarding division in Python 2, there is by default only / which maps to __div__ and the result is dependent on the input types (e.g. int, float).

Python 2.2 introduced the __future__ feature division, which changed the division semantics the following way (TL;DR of PEP 238):

  • / maps to __truediv__ which must "return a reasonable approximation of the mathematical result of the division" (quote from PEP 238)
  • // maps to __floordiv__, which should return the floored result of /

With Python 3.0, the changes of PEP 238 became the default behaviour and there is no more special method __div__ in Python's object model.

If you want to use the same code in Python 2 and Python 3 use

from __future__ import division

and stick to the PEP 238 semantics of / and //.

@Brandon E Taylor 2009-08-15 21:51:44

Take a look at PEP-238: Changing the Division Operator

The // operator will be available to request floor division unambiguously.

@thang 2017-09-11 20:22:49

@JonathanSternberg except for all the code that was written for python 2.0. I feel like the role of / and // should be reversed to keep backwards compatibility. Also, in pretty much every other language / preserves type. i would make more sense then for // to automatically convert to float, not /.

@Jonas Byström 2009-08-15 21:50:14

Oops, immediately found 2//2.

@dangonfast 2017-10-09 08:13:14

Could you elaborate?

@Ashish Ahuja 2018-05-21 05:02:12

Using this will output an int, not a float @dangonfast.

@mschuett 2019-03-25 01:25:37

This seems to not work for negative numbers. -1//5 return -1 and -5//5 returns -1.

@Jonas Byström 2019-03-26 14:56:26

@mschuett: this is to be expected, and is the same result as with Python 2.

@mschuett 2019-03-26 15:19:18

Yeah I did some more digging and found that out as well. However I would imagine a decent size number of people do not realize this depending on the language they come from.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] How do I parse a string to a float or int in Python?

42 Answered Questions

[SOLVED] What does the "yield" keyword do?

56 Answered Questions

[SOLVED] Calling an external command in Python

13 Answered Questions

63 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3765972 View
  • 3185 Score
  • 63 Answer
  • Tags:   java random integer

15 Answered Questions

[SOLVED] What are metaclasses in Python?

28 Answered Questions

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

22 Answered Questions

[SOLVED] Convert a string to an integer in JavaScript?

17 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

Sponsored Content