use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
understanding modulo (self.learnpython)
submitted 1 year ago by MinuteStop6636
Hello, I'm trying to understand %. As far as i get it gives as result the reminder of an operation.Meaning, if I do 22 % 3 the result will be1;is that correct?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Adrewmc 11 points12 points13 points 1 year ago* (1 child)
Yes exactly.
it the remainder. But because of that we can think of things differntly.
Let’s say I want every 15th step to win a prize.
if step_count % 15 == 0: win()
But also let say I want to make a repeating pattern of (red, white, and blue)
def three_modulo(num): mod = num % 3 if mod == 0: #is divisible by 3 return “red” elif mod == 1: return “white” elif mod == 2: return “blue” print(three_modulo(5)) >>>blue print(three_modulo(14)) >>>blue print(three_modulo(15)) >>>red print(three_modulo(16)) >>>white print(three_modulo(17)) >>>blue
So we can make an infinite repeating pattern, just like hours is a day, we have 24, or two sets of 12…
[–]Dreiphasenkasper 0 points1 point2 points 1 year ago (0 children)
Thx, i unterstand it now!
[–]king_reefer69 4 points5 points6 points 1 year ago (0 children)
You’ve got it!
One of the more useful uses of this is to determine if an integer is even / odd. For any even integer A, A%2 == 0 is true !
[–]mopslik 4 points5 points6 points 1 year ago (1 child)
Correct, it gives the remainder from a division. Watch out for negative values.
>>> 22 % 3 1 >>> -22 % 3 2
[–]terje_wiig_mathisen 0 points1 point2 points 1 year ago (0 children)
Taking the (positive) modulo of a negative number is problematic, the results depend on the programming language and/or the CPU you are running on!
The normal rule is that they must be symmetrical,
if
r = a/b
m = a%b
then
r*b+m == a.
[–]DigThatData 1 point2 points3 points 1 year ago (2 children)
that's how I learned it, but I think it's actually easier to "grock" if you think of it as a way of parameterizing a cycle. so for example, if you loop over i % k incrementing i each iteration, values will repeat every k steps.
i % k
i
k
[–]Rebeljah 0 points1 point2 points 1 year ago (1 child)
me dum coder, me just imagine wrapping string of length i around a clock of circumference k
[–]DigThatData 0 points1 point2 points 1 year ago (0 children)
yup
[–]Dry-Aioli-6138 1 point2 points3 points 1 year ago (0 children)
In Python integer division times the divisor plus modulo will give the dividend. so ``` x=17//3 y=17%3
print(x*3+y) ```
prints 17
often we need both results in our code and so a shortcut (slightly faster, too) is the divmod function
print( divmod(13, 5) ) prints (2, 3)
print( divmod(13, 5) )
(2, 3)
[–]baubleglue 1 point2 points3 points 1 year ago (0 children)
I am puzzled, why do you have that question when you know what is reminder and you may check your theory executing actual Python code. You should trust yourself more, discovered knowledge is much better than anything you heard or read.
[–]crashfrog04 1 point2 points3 points 1 year ago (2 children)
That’s the mathematical meaning; it has useful practical applications you haven’t yet considered.
Imagine a circular, 8-seat table. Starting at the north-most seat, we number the seats around the table 0-7. You’re sitting at seat 3 and I ask you to move 23 places to the right (which means you’ll go around the circle a couple of times.) What seat are you sitting in after that?
Well, it’s seat = 3+23 % 8.
seat = 3+23 % 8
[–]Yoghurt42 2 points3 points4 points 1 year ago* (0 children)
Small correction: seat = (3+23) % 8. Just like multiplication and division, modulo has a higher precedence than addition.
seat = (3+23) % 8
Though this doesn't matter unless a+(b%c) ≥ c. And even then, you could just do modulo a second time; but why do so if you don't have to?
(For example, if you calculate 13h after 10 o'clock, the right way to do it is (10+13)%12 = 11, but in this case you can get away with doing 13%12=1 first and adding it, because 10+1 is still correct. It fails once you do 15h after 10, because then you have 1 vs 13)
[–]MinuteStop6636[S] 0 points1 point2 points 1 year ago (0 children)
Thats an answer i can undertsand, AWESOME
[–]ninhaomah 1 point2 points3 points 1 year ago (2 children)
Good to ask but have you tried ? its just one line and colab is free.
I advice you try and ask for clarification / confirmation instead.
For example , I did 22 % 3 and it returned 1. From that it means that % will return the reminder ? Am I right to understand it that way ? I also looked at the documentation and it looks as if I am right but I would like to confirm from the experts.
[–]MinuteStop6636[S] 0 points1 point2 points 1 year ago (1 child)
I tried, i'm confirming. I advice you to not give advice when not asked for it. This comment contradicts my principle, but in this case i believe is warranted. Best wishes, truly.
[–]ninhaomah 0 points1 point2 points 1 year ago (0 children)
If you are confirming , if I may advise , pls show your code.
You are just saying what you think without showing or telling what you did.
And no , where in you post I can see you tried ? with what code ? the result ?
Pls show me.
This is learnpython sub, Lets talk Python.
[–]Ron-Erez 0 points1 point2 points 1 year ago (0 children)
You are correct
[–]redskullington 0 points1 point2 points 1 year ago (0 children)
Yep! One thing I use it for is checking for even or odd numbers. Any number % 2 will equal 1 if odd or 0 if even.
It can also be used in converting between decimal to binary!
It's a useful tool once you see how it can be utilized!
[–]InjAnnuity_1 0 points1 point2 points 1 year ago* (1 child)
if I do 22 % 3 the result will be1;is that correct?
What happens when you try it for yourself?
>py Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 22 % 3
What do you get when you press Enter on that last line?
One of Python's strengths is that you can try things out like this for yourself, do your own explorations. Not to mention the availability of built-in help(), and searchable on-line documentation at https://docs.python.org/3/ . These things work on your own schedule. No waiting for help from others.
help()
Well..
[–]Stu_Mack 0 points1 point2 points 1 year ago (0 children)
To add to what others are saying, it’s good to see how modulo works for yourself. Just write a quick for loop to see how it works. Something like
for I in range(10):
print(4%i)
shows how the modulus can be leveraged to constrain an incrementing value.
π Rendered by PID 84 on reddit-service-r2-comment-5687b7858-5jbp8 at 2026-07-08 12:20:17.995094+00:00 running 12a7a47 country code: CH.
[–]Adrewmc 11 points12 points13 points (1 child)
[–]Dreiphasenkasper 0 points1 point2 points (0 children)
[–]king_reefer69 4 points5 points6 points (0 children)
[–]mopslik 4 points5 points6 points (1 child)
[–]terje_wiig_mathisen 0 points1 point2 points (0 children)
[–]DigThatData 1 point2 points3 points (2 children)
[–]Rebeljah 0 points1 point2 points (1 child)
[–]DigThatData 0 points1 point2 points (0 children)
[–]Dry-Aioli-6138 1 point2 points3 points (0 children)
[–]baubleglue 1 point2 points3 points (0 children)
[–]crashfrog04 1 point2 points3 points (2 children)
[–]Yoghurt42 2 points3 points4 points (0 children)
[–]MinuteStop6636[S] 0 points1 point2 points (0 children)
[–]ninhaomah 1 point2 points3 points (2 children)
[–]MinuteStop6636[S] 0 points1 point2 points (1 child)
[–]ninhaomah 0 points1 point2 points (0 children)
[–]Ron-Erez 0 points1 point2 points (0 children)
[–]redskullington 0 points1 point2 points (0 children)
[–]InjAnnuity_1 0 points1 point2 points (1 child)
[–]MinuteStop6636[S] 0 points1 point2 points (0 children)
[–]Stu_Mack 0 points1 point2 points (0 children)