alsaie
alsaie

Reputation: 11

Is there a bug in integer division (%) Python?

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

7%5 2
8%5 3
6%5 1

Upvotes: 0

Views: 226

Answers (2)

Kadeeja Bai
Kadeeja Bai

Reputation: 72

You are using Modulo operation 7%5 = 2 its correct For integer division you have to use "//" eg: 7//5.If you want result in float you can use 7/5 .

Upvotes: 0

vomi
vomi

Reputation: 1163

Division /

With integers as input, the division will give you an integer result (Python2+)

>>> 7/5
1
>>> 8/5
1
>>> 10/5
2

With at least one non-integer as input (Python2+), you will have a non-integer result.

>>> 10/1.5
6.666666666666667
>>> 10/4.0
2.5

Python3: The integer division is //

>>> 10/4 # Python 3
2.5
>>> 10//4 # Python 3 integer division
2

Modulo % (= remainder)

The modulo is the remainder of the integer division

>>> 7%5 # = What is the remainder of the division of 7 by 5
2
>>> 8%5
3
>>> 10%5
0

Upvotes: 1

Related Questions