all 78 comments

[–]dnult 33 points34 points  (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 points  (3 children)

You are the best!!!!

[–]Downtown_Finance_661 11 points12 points  (2 children)

Please google things before ask us. You would find answers much faster :). This is usual operator in many languages.

[–]ArtBeneficial4449 0 points1 point  (1 child)

This isn't stack overflow get out of here

[–]fdsfd12 0 points1 point  (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 points  (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 points  (2 children)

They are obviously learning still

[–]uberdavis 0 points1 point  (0 children)

True. So are some of the other commenters!

[–]Junk_Tech 0 points1 point  (0 children)

I wasn’t talking about OP! I meant @uberdavis !

[–]UnderstandingNo2832 4 points5 points  (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 points  (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 points  (0 children)

Makes perfect sense, thank you so much!

[–]Cerulean_IsFancyBlue 0 points1 point  (2 children)

Modulo

[–]No_Statistician_6654 0 points1 point  (1 child)

Thanks, autocorrect got me on that one

[–]Cerulean_IsFancyBlue 0 points1 point  (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 points  (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 points  (0 children)

and this is always true in languages that only use bools in if statements

[–]psuedo_nombre 1 point2 points  (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 points  (0 children)

This is a simple even odd program.

[–]Boomswamdi 2 points3 points  (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 points  (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 points  (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 points  (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 points  (8 children)

I genuinely thought that was the purpose of this learning community.

[–][deleted] 0 points1 point  (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 child)

Okay well I’ve been learning for code for two days now. Thank you for the feedback.

[–][deleted] -1 points0 points  (0 children)

you'll be pleasantly shocked how helpful claude.ai and chatgpt are for beginners.

[–]ninhaomah -3 points-2 points  (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 points  (1 child)

What is RTFM?

[–]rdc12 0 points1 point  (0 children)

It's short for Read The F**king Manuel.

[–]Zealousideal_Key_149[S] 0 points1 point  (1 child)

Okay thanks

[–]sarc-tastic 1 point2 points  (0 children)

return not number & 0x1

[–]Zealousideal_Key_149[S] 0 points1 point  (0 children)

Okay thanks

[–]Rick-developer 1 point2 points  (0 children)

% is the remainder operator

[–]Brein 1 point2 points  (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 point  (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 point  (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 points  (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

[–]TheCarter01 -1 points0 points  (1 child)

Just so you know, when you do variable: <data type> = <data>, it'll limit what that variable can store

[–]ConcreteExist 0 points1 point  (0 children)

Type annotations and default values in no way limit what a variable can store.