How to Use Integer Division in Python

Learn to divide and ignore the remainder

Jonathan Hsu
Code 85

--

Photo by Dan Meyers on Unsplash

What is Integer Division?

Integer division is an arithmetic operation where division is performed, but the remainder is discarded leaving us with just an integer.

In other programming languages, the combination of division plus something akin to a “floor” function is used to achieve similar results.

Here’s an example in JavaScript:

quotient = 8 / 3;
whole_number = Math.floor(quotient);
console.log(whole_number); // prints 2

Having to round-down a normal division operation may not be particularly difficult, but it can be annoying and inconvenient. Fortunately, Python has us covered with the integer division operator.

Integer Division in Python

Many popular languages such as JavaScript, Java, and PHP use the double forward slash // as a comment operator. However, in Python this simple operator is in fact used for integer division.

There isn’t too much to say, so let’s take a look at an example.

a = 8
b = 3
quotient = a / b
whole_number = a // b
print(quotient, type(quotient))…

--

--