174

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,738
  • 21
  • 105
  • 131
Megatron
  • 15,909
  • 12
  • 89
  • 97

1 Answers1

266

Try this:

a = 1
b = 2
int_div  = a // b
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Lucas Ribeiro
  • 6,132
  • 2
  • 25
  • 28
  • 12
    Note that `//` is available in Python2 as well (since 2.2, I believe). – Kyle Strand Jan 14 '16 at 17:57
  • 26
    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
  • 34
    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