The Python Oracle

How to calculate a mod b in Python?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping

--

Chapters
00:00 How To Calculate A Mod B In Python?
00:14 Accepted Answer Score 263
00:25 Answer 2 Score 11
00:34 Answer 3 Score 28
00:56 Answer 4 Score 43
01:04 Thank you

--

Full question
https://stackoverflow.com/questions/9910...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 263


There's the % sign. It's not just for the remainder, it is the modulo operation.




ANSWER 2

Score 43


>>> 15 % 4
3
>>>

The modulo gives the remainder after integer division.




ANSWER 3

Score 28


mod = a % b

This stores the result of a mod b in the variable mod.

And you are right, 15 mod 4 is 3, which is exactly what python returns:

>>> 15 % 4
3

a %= b is also valid.




ANSWER 4

Score 11


Why don't you use % ?


print 4 % 2 # 0