Python 3 integer division

173,211

Try this:

a = 1
b = 2
int_div  = a // b
Share:
173,211

Related videos on Youtube

Megatron
Author by

Megatron

BY DAY: Mining energon, battling Autobots, planning triumphant return to Cybertron. FOR FUN: r analyses of any publicly available data, python for ML, git for all of the above "If something cannot go on forever, it will stop." - Herbert Stein's Law "The perfect is the enemy of the good." "A camel is a horse designed by committee." "All models are wrong, but some are useful." - George Box "There are three kinds of lies: lies, damned lies and statistics." "If you torture the data long enough, it will confess to anything." "The enemy of art is the absence of limitations." - Orson Welles "If you're explaining, you're losing." "Excessive civility without adequate candor means that everyone feels good but not much is really being discussed. Candor is more proactive." - Todd Dewett "Five minutes of extra care saves an hour" "I have not failed. I've just found 10,000 ways that won't work." - Thomas Edison

Updated on January 31, 2022

Comments

  • Megatron
    Megatron over 2 years

    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?

    • falsetru
      falsetru over 10 years
      Use // (floor division) instead of / (true division).
    • Seng Cheong
      Seng Cheong over 10 years
      PEP 238 introduced the // floor division operator.
  • Kyle Strand
    Kyle Strand over 8 years
    Note that // is available in Python2 as well (since 2.2, I believe).
  • asmaier
    asmaier over 6 years
    Note that 1.0 // 2 and 1 // 2.0 maybe surprisingly return a float with value 0.0.
  • Thorham
    Thorham almost 5 years
    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).