177

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?

2
  • 30
    Use // (floor division) instead of / (true division). Commented Oct 22, 2013 at 2:03
  • 7
    PEP 238 introduced the // floor division operator. Commented Oct 22, 2013 at 2:15

1 Answer 1

271

Try this:

a = 1
b = 2
int_div  = a // b
Sign up to request clarification or add additional context in comments.

3 Comments

Note that // is available in Python2 as well (since 2.2, I believe).
Note that 1.0 // 2 and 1 // 2.0 maybe surprisingly return a float with value 0.0.
Floor divisions are NOT integer divisions. A floor division will return -2 for -3 / 2, while an integer division should return -1 (there's no floor or ceil in integer land).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.