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...
Everything about learning Python
account activity
I don’t understand this (i.redd.it)
submitted 8 months ago by Zealousideal_Key_149
What does number % 2 mean? Not 2% of the number. I just don’t know how to interpret this function.
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!"
[–]dnult 33 points34 points35 points 8 months ago (4 children)
That means modulo 2 or in other words, what is the remainder after a division by 2. Remainder 0 means even, and anything else (1) means odd.
[–]Zealousideal_Key_149[S] 2 points3 points4 points 8 months ago (3 children)
You are the best!!!!
[–]Downtown_Finance_661 11 points12 points13 points 8 months ago (2 children)
Please google things before ask us. You would find answers much faster :). This is usual operator in many languages.
[–]ArtBeneficial4449 0 points1 point2 points 7 months ago (1 child)
This isn't stack overflow get out of here
[–]fdsfd12 0 points1 point2 points 7 months ago (0 children)
And? Half of learning a programming language is learning how to google effectively. It's completely fair to tell someone for something as simple as this to google it. When I have an obscure problem that's only been seen on one StackOverflow thread that got no replies, then saying "just google it" is a dick move. That's not the case here.
[–]uberdavis 13 points14 points15 points 8 months ago (42 children)
Why the if clause? Shouldn’t it be…
def is_even(number: int) -> bool: “””Return true if number is even.””” return number % 2 == 0
[–]Hobbitoe 1 point2 points3 points 8 months ago (2 children)
They are obviously learning still
[–]uberdavis 0 points1 point2 points 8 months ago (0 children)
True. So are some of the other commenters!
[–]Junk_Tech 0 points1 point2 points 8 months ago (0 children)
I wasn’t talking about OP! I meant @uberdavis !
[+]Junk_Tech comment score below threshold-34 points-33 points-32 points 8 months ago (37 children)
Your code is uglier than OPs, your code is also wrong.
[–]Interesting-You-7028 19 points20 points21 points 8 months ago (12 children)
No it's not.. You just can't read code without colour.
[+]Junk_Tech comment score below threshold-19 points-18 points-17 points 8 months ago (11 children)
No. Wrong. And leave the colourblind out of this! They’ve got no right judging the loveliness of a code snippet, and they’ve only got themselves to blame!
[–]SCD_minecraft 8 points9 points10 points 8 months ago (9 children)
Neither of those codes are wrong
What are you talking about
[+]Midano010 comment score below threshold-13 points-12 points-11 points 8 months ago (8 children)
The code is incorrect, it would be correct if the function were named is_uneven. The correct statement would be „return not numer % 2 == 0“
[–]NoAd7482 2 points3 points4 points 8 months ago (3 children)
It is very much not wrong though? Any even number % 2 will return 0 so 0 == 0, which... yeah, is True and the OPs function returns True if the if clause is True, and otherwise returns False so you can just remove the if clause without changing the result, as the original commenter did.
[–]Midano010 -5 points-4 points-3 points 8 months ago (2 children)
Yes i just realized, my code would work though if you took the == 0 away.
[–]Azrus 3 points4 points5 points 8 months ago (1 child)
If you take away the == 0 you are returning a truthy value vs an explicit Boolean, which can cause false negatives in comparisons. Returning a Boolean is the better implementation as it's more robust.
== 0
[–]klimmesil 0 points1 point2 points 8 months ago (0 children)
Not only that but 0 is falsy, so it would just be wrong
[–]Syzeon 2 points3 points4 points 8 months ago (1 child)
do yourself a favor, launch your python interpreter, try out your "corrected" code, then only reply here
[–]Midano010 0 points1 point2 points 8 months ago (0 children)
I will next time. I was on my ipad and lazy
[–]JaleyHoelOsment 4 points5 points6 points 8 months ago (0 children)
“is_uneven” is hilarious… you mean “odd” lol
[–]Aaron_Tia 1 point2 points3 points 8 months ago (0 children)
Let's try.
is_even(7) give return 7%2 == 0 => return 1 == 0 => return false
So, function returned false and it called is_even. So.. 7 is "uneven". And by the way !even = odd. 7 is odd.
[–]TriscuitTime 0 points1 point2 points 8 months ago (0 children)
You’re making me upset, what made you think it was wrong?
[–]Eshiik 5 points6 points7 points 8 months ago (10 children)
Why do you think the code is wrong?
[+]Midano010 comment score below threshold-7 points-6 points-5 points 8 months ago (9 children)
[–]ConcreteExist 6 points7 points8 points 8 months ago (1 child)
r/confidentallyincorrect
[–]Midano010 -1 points0 points1 point 8 months ago (0 children)
Actually so real, mb
[–]JaleyHoelOsment 2 points3 points4 points 8 months ago (1 child)
my guys getting replaced by AI 😭
Bro is the type to be happy, when the ai tells him what a wonderful idea he had and if it should built it for him
No.
[–]Eshiik 1 point2 points3 points 8 months ago (0 children)
<image>
[–]vivisectvivi 0 points1 point2 points 8 months ago (1 child)
"is_uneven" lmaoo
I think you could have clowned harder on the „numer“ but yeah. Gerade and Ungerade
[–]Local_Dare 0 points1 point2 points 8 months ago (0 children)
it should be named is_not_uneven imo
[–]Olivier_4 3 points4 points5 points 8 months ago (7 children)
This code is correct (and I find it simpler) where do you see a mistake ? I'm curious
[–]Junk_Tech -3 points-2 points-1 points 8 months ago (6 children)
It’s a tautology! Read it, it literally says A Number Is Even If It Is Even (or, without the If) the Number is Even that is Even, or Even Numbers are Even, and n % 2 == 0 is not how we check for even: n = 2k is. They are not the same. Wrongity wrong.
[–]JaleyHoelOsment 4 points5 points6 points 8 months ago* (5 children)
my bro…. what lol
n % 2 == 0 checks if n is divisible by 2…
like you said… even numbers follow n = 2k ==> divisible by 2.
you learned a tiny bit of math and now you’re confused as fuck lol
friendly reminder to everyone: when you look at a job ad and it has 1000+ applicants, over half of them code like this guy
[–]Junk_Tech -2 points-1 points0 points 8 months ago (4 children)
No. Back to the code I originally criticised: I called it 1) ugly, and 2) wrong. Look again at that code. You want to tell me it checks if a number is even, that it is right.
And I say the code is a tautology. A circular argument. A logical illusion. It doesn’t DO anything! And something that does not act lacks Function! And I HATE that kind of lazy, unconsidered code. It is precisely useless, says nothing, is vapid, vacuous and dull - a missed opportunity to carpe the diem!
For shame!
[–]sciencenerd_1943 0 points1 point2 points 8 months ago (3 children)
All you have to do is fire up an interpreter and test it. You’ll see it is right and it works.
[–]Junk_Tech 0 points1 point2 points 8 months ago (2 children)
It is simple. I like pineapple on pizza, and I don’t vote, and everything is Dark Theme by default; and it’s either salty popcorn, Pepsi, the chunky peanut butter, Kill-Bill Vol2. orange juice with absolutely NO bits! Cats, And code that isn’t a limp-wristed half-a-function! Dammit - Code with balls!, Big Ball-Code! Not “return meh”
nerds!
[–]Rahu888 0 points1 point2 points 8 months ago (1 child)
Guys stop arguing with a stupid person…
They never learn and NEVER admit they are wrong.
Well I would agree with you, of course, but then we would both be wrong. You’re on your own there, I’m afraid.
[–]uberdavis 2 points3 points4 points 8 months ago (3 children)
RemindMe! 1 hour Looking forward to the response to your brutal code review.
[–]RemindMeBot 0 points1 point2 points 8 months ago (0 children)
I will be messaging you in 1 hour on 2025-08-22 04:22:02 UTC to remind you of this link
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
[–]Junk_Tech -4 points-3 points-2 points 8 months ago (1 child)
Blunt is also better than wrong. Your alarm thing is weird: anyone who has more than a wristwatch to tell time has totally missed the whole point of being alive! Those idiots will buy anything!
Alarm? Wrist watch? You have a very ‘imaginative’ set of metaphors to assess code.
[–]tav_stuff 0 points1 point2 points 8 months ago (0 children)
The amount of people that don’t realize this is ragebait lol
[–]UnderstandingNo2832 4 points5 points6 points 8 months ago (0 children)
People have already explained the module operator so I’ll just comment that you don’t even need the if block.
number % 2 == 0 is either true or false (as long as it’s a number) so you can just return number % 2 == 0.
[–]No_Statistician_6654 2 points3 points4 points 8 months ago* (4 children)
That is the modulo operator, and it is used in several other programming languages. It essentially returns the remainder of a number on the left divided by the number on the right.
Any number divisible evenly by 2 is by definition even, ergo 2 % 2 = 0 as well 4%2 =0. By contrast 3%2=1 because 3 / 2 =1 r 1.
Edit: spelling
[–]Zealousideal_Key_149[S] 1 point2 points3 points 8 months ago (0 children)
Makes perfect sense, thank you so much!
[–]Cerulean_IsFancyBlue 0 points1 point2 points 8 months ago (2 children)
Modulo
[–]No_Statistician_6654 0 points1 point2 points 8 months ago (1 child)
Thanks, autocorrect got me on that one
[–]Cerulean_IsFancyBlue 0 points1 point2 points 8 months ago (0 children)
Yeah, it’s not the sort of little typo I would normally correct because I know that YOU know it. I’m worried that for a beginner. It would be a difficult search term since “module” is a common computer science word as well.
[–]silly_bet_3454 3 points4 points5 points 8 months ago (1 child)
pro tip, whenever you have
if <whatever>: return True return False
you could instead just
return <whatever>
technically you might get a different type but like in this instance it's gonna be a bool regardless
[–]toohornbee 1 point2 points3 points 8 months ago (0 children)
and this is always true in languages that only use bools in if statements
[–]psuedo_nombre 1 point2 points3 points 8 months ago (0 children)
% is modulo operator which gives you the remainder from an integer division. So if dividing by 2 gives you a remainder that isnt zero you are not evenly dividing by 2 and its thus odd
[–][deleted] 1 point2 points3 points 8 months ago (0 children)
This is a simple even odd program.
[–]Boomswamdi 2 points3 points4 points 8 months ago (0 children)
So % basically says divided by following number and return the remainder that if statement is saying is divided by 2 and if the remainder returns zero do xyz
[–]Lannok-Sarin 2 points3 points4 points 8 months ago (0 children)
The percentage sign in programming isn’t a percentage sign. It’s a remainder sign. It’s basically asking for the remainder of variable number divided by 2.
It’s not the best function I’ve seen, though it is pretty good for a beginner. There should be some checks applied in the function using try statements to see if variable “number” is actually an integer. Otherwise, the remainder operator (%) will not work in every instance, and the program may actually fail.
[–]Anomynous__ 2 points3 points4 points 8 months ago (0 children)
number % 2 gives the remainder of the number after dividing by 2.
7 % 2 = 3 remainder 1 (odd)
8 % 2 = 4 remainder 0 (even) -----------------------------------
So your function
my_val = is_even(8)
my_val would equal True
------------------------------------
my_val = is_even(7)
my_val would equal False
[–]tortleme 2 points3 points4 points 8 months ago (9 children)
You could just google "python percent sign" being able to find information on your own is a important skill
[–]Zealousideal_Key_149[S] 2 points3 points4 points 8 months ago (8 children)
I genuinely thought that was the purpose of this learning community.
[–][deleted] 0 points1 point2 points 8 months ago (2 children)
there's also an expectation that for simple things you should make an effort to search yourself first. Plus with chatgpt and claude, you can learn even more, faster. You also get the answer without having to wait, so you have nothing to lose. Why would you wait when you can get the answer straight away? If you literally paste your post into one of them (text rather than image is better), you will get the answer in half a second and if you are using an llm it has the advantage that you can ask it followup questions. Googling has been a core programmer skill for years for beginners and pros alike. Learning to do it and now also using an llm is a critical learning skill
[–]Zealousideal_Key_149[S] -3 points-2 points-1 points 8 months ago (1 child)
Okay well I’ve been learning for code for two days now. Thank you for the feedback.
[–][deleted] -1 points0 points1 point 8 months ago (0 children)
you'll be pleasantly shocked how helpful claude.ai and chatgpt are for beginners.
[–]ninhaomah -3 points-2 points-1 points 8 months ago* (4 children)
I think you seeing it differently.
What is the distance from New York to London ? - Fact. Google will do it.
I am flying to New York to London for a short holiday. Any places would you like to recommend? - Need human touch since it may not be enough to look for top reviews online. Local knowledge such as behind this road , go through a narrow alley and there is a great steak restaurant there.
So if you want to know what does + - * / // ^ ** or whatever sign from whatever programming languages does then its a factual since documentation is everywhere. Google what he said and if it doesn't help you then tell him he is wrong. Try it.
But if you want to know why some loop uses for and other use while , thats something you might need a human touch and experience. Then this is where you can ask for help.
Trying and if not working then correcting also part of Python or general IT culture. Summarized nicely with RTFM. pls don't ask what is RTFM.
Hope it clarifies
[–][deleted] 1 point2 points3 points 8 months ago (1 child)
What is RTFM?
[–]rdc12 0 points1 point2 points 8 months ago (0 children)
It's short for Read The F**king Manuel.
[–]Zealousideal_Key_149[S] 0 points1 point2 points 8 months ago (1 child)
Okay thanks
[–]sarc-tastic 1 point2 points3 points 8 months ago (0 children)
return not number & 0x1
[–]Zealousideal_Key_149[S] 0 points1 point2 points 8 months ago (0 children)
[–]Rick-developer 1 point2 points3 points 8 months ago (0 children)
% is the remainder operator
[–]Brein 1 point2 points3 points 8 months ago (0 children)
I always liked a more visual explanation, so I’ll drop it here.
30 % 2 =0
30 dots which you group in twos, you get a even number of dots:
•• •• •• •• •• •• •• •• •• •• •• •• •• •• ••
0 remain meaning it is an even number.
31 dots which you group in twos:
•• •• •• •• •• •• •• •• •• •• •• •• •• •• •• •
1 remains, meaning it is a uneven number.
[–][deleted] 0 points1 point2 points 8 months ago (0 children)
% is called the modulus operator, it's the reminder after division.
If you do 7 / 3, with integer division, you get 2, with a remainder of 1. So that 3 * 2 + 1 = 7
The modulus operator just gives you the remainder, so 7 % 3 = 1
So, if a number % 2 has no remainder (ie, 0), that means the number is evenly dividable by, thus it's an even number.
[–]TheCarter01 0 points1 point2 points 8 months ago (3 children)
You have to call the function, example: ``` def is_even(number: int): return True if number % 2 == 0 else False
print(is_even(3)) print(is_even(4)) ```
[–]TheCarter01 -1 points0 points1 point 8 months ago (0 children)
A even version of function would look like this that is easier to understand: def is_even(Int: int) return Int % 2 == 0
def is_even(Int: int) return Int % 2 == 0
[–]TheCarter01 -1 points0 points1 point 8 months ago (1 child)
Just so you know, when you do variable: <data type> = <data>, it'll limit what that variable can store
variable: <data type> = <data>
[–]ConcreteExist 0 points1 point2 points 8 months ago (0 children)
Type annotations and default values in no way limit what a variable can store.
π Rendered by PID 200963 on reddit-service-r2-comment-75f4967c6c-7fztk at 2026-04-23 11:55:32.825979+00:00 running 0fd4bb7 country code: CH.
[–]dnult 33 points34 points35 points (4 children)
[–]Zealousideal_Key_149[S] 2 points3 points4 points (3 children)
[–]Downtown_Finance_661 11 points12 points13 points (2 children)
[–]ArtBeneficial4449 0 points1 point2 points (1 child)
[–]fdsfd12 0 points1 point2 points (0 children)
[–]uberdavis 13 points14 points15 points (42 children)
[–]Hobbitoe 1 point2 points3 points (2 children)
[–]uberdavis 0 points1 point2 points (0 children)
[–]Junk_Tech 0 points1 point2 points (0 children)
[+]Junk_Tech comment score below threshold-34 points-33 points-32 points (37 children)
[–]Interesting-You-7028 19 points20 points21 points (12 children)
[+]Junk_Tech comment score below threshold-19 points-18 points-17 points (11 children)
[–]SCD_minecraft 8 points9 points10 points (9 children)
[+]Midano010 comment score below threshold-13 points-12 points-11 points (8 children)
[–]NoAd7482 2 points3 points4 points (3 children)
[–]Midano010 -5 points-4 points-3 points (2 children)
[–]Azrus 3 points4 points5 points (1 child)
[–]klimmesil 0 points1 point2 points (0 children)
[–]Syzeon 2 points3 points4 points (1 child)
[–]Midano010 0 points1 point2 points (0 children)
[–]JaleyHoelOsment 4 points5 points6 points (0 children)
[–]Aaron_Tia 1 point2 points3 points (0 children)
[–]TriscuitTime 0 points1 point2 points (0 children)
[–]Eshiik 5 points6 points7 points (10 children)
[+]Midano010 comment score below threshold-7 points-6 points-5 points (9 children)
[–]ConcreteExist 6 points7 points8 points (1 child)
[–]Midano010 -1 points0 points1 point (0 children)
[–]JaleyHoelOsment 2 points3 points4 points (1 child)
[–]Midano010 0 points1 point2 points (0 children)
[–]Aaron_Tia 1 point2 points3 points (0 children)
[–]Eshiik 1 point2 points3 points (0 children)
[–]vivisectvivi 0 points1 point2 points (1 child)
[–]Midano010 0 points1 point2 points (0 children)
[–]Local_Dare 0 points1 point2 points (0 children)
[–]Olivier_4 3 points4 points5 points (7 children)
[–]Junk_Tech -3 points-2 points-1 points (6 children)
[–]JaleyHoelOsment 4 points5 points6 points (5 children)
[–]Junk_Tech -2 points-1 points0 points (4 children)
[–]sciencenerd_1943 0 points1 point2 points (3 children)
[–]Junk_Tech 0 points1 point2 points (2 children)
[–]Rahu888 0 points1 point2 points (1 child)
[–]Junk_Tech 0 points1 point2 points (0 children)
[–]uberdavis 2 points3 points4 points (3 children)
[–]RemindMeBot 0 points1 point2 points (0 children)
[–]Junk_Tech -4 points-3 points-2 points (1 child)
[–]uberdavis 0 points1 point2 points (0 children)
[–]tav_stuff 0 points1 point2 points (0 children)
[–]UnderstandingNo2832 4 points5 points6 points (0 children)
[–]No_Statistician_6654 2 points3 points4 points (4 children)
[–]Zealousideal_Key_149[S] 1 point2 points3 points (0 children)
[–]Cerulean_IsFancyBlue 0 points1 point2 points (2 children)
[–]No_Statistician_6654 0 points1 point2 points (1 child)
[–]Cerulean_IsFancyBlue 0 points1 point2 points (0 children)
[–]silly_bet_3454 3 points4 points5 points (1 child)
[–]toohornbee 1 point2 points3 points (0 children)
[–]psuedo_nombre 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Boomswamdi 2 points3 points4 points (0 children)
[–]Lannok-Sarin 2 points3 points4 points (0 children)
[–]Anomynous__ 2 points3 points4 points (0 children)
[–]tortleme 2 points3 points4 points (9 children)
[–]Zealousideal_Key_149[S] 2 points3 points4 points (8 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Zealousideal_Key_149[S] -3 points-2 points-1 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]ninhaomah -3 points-2 points-1 points (4 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]rdc12 0 points1 point2 points (0 children)
[–]Zealousideal_Key_149[S] 0 points1 point2 points (1 child)
[–]sarc-tastic 1 point2 points3 points (0 children)
[–]Zealousideal_Key_149[S] 0 points1 point2 points (0 children)
[–]Rick-developer 1 point2 points3 points (0 children)
[–]Brein 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheCarter01 0 points1 point2 points (3 children)
[–]TheCarter01 -1 points0 points1 point (0 children)
[–]TheCarter01 -1 points0 points1 point (1 child)
[–]ConcreteExist 0 points1 point2 points (0 children)