168

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Megatron
  • 14,141
  • 11
  • 84
  • 94

1 Answers1

254

Try this:

a = 1
b = 2
int_div  = a // b
Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
Lucas Ribeiro
  • 5,864
  • 2
  • 23
  • 28
  • 12
    Note that `//` is available in Python2 as well (since 2.2, I believe). – Kyle Strand Jan 14 '16 at 17:57
  • 25
    Note that `1.0 // 2` and `1 // 2.0` maybe surprisingly return a float with value `0.0`. – asmaier Sep 12 '17 at 09:09
  • 31
    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). – Thorham Jun 14 '19 at 10:04