Skip to content

Python integer division

  • by

Python Integer division is the division of one integer by another in which the resulting number is truncated (ie. decimal places are dropped).

If you want to use integer division instead of the standard float division, we can use the slash // operator:

print(3 / 2)  # prints 1.5

print(3 // 2)  # prints 1

Which is the same as:

import math
math.floor(15 / 4)

Python integer division

A simple example code gets two integer numbers input, divides both integers and displays the Integer quotient.

num1 = int(input("Enter First number:"))
num2 = int(input("Enter second number:"))

quotient = num1 // num2
print(quotient)

Output:

Python integer division

/ vs // — division vs floor division

The result of regular division (using the / operator) is 15/4 = 3.75 , but using // has floored 3.75 down to 3. Integer division always rounds towards negative infinity, meaning that the result will be rounded down to the nearest integer if the result is positive, and rounded up to the nearest integer if the result is negative.

Do comment if you have any doubts or suggestions on this Python integer topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading