Python Practice Creating and Using Functions in Python Practice Functions That's Odd

Charles Magee
Charles Magee
Python Development Techdegree Student 554 Points

Hi,I tried looking for help online but Im yet to come across any websites the can help me more understand this function

create_a_function.py
def is_odd num:
    if num % 2 != 0:
        returns True
        else returns False
Jeremy Charles
Jeremy Charles
Pro Student 643 Points

Hi The % sign is known as the modulo operator. The modulo operator returns the remainder of a division operation. example: 2 goes into 4 two times, leaving no remainder however 2 goes into 3 once leaving a remainder of 1 The remainder: 1, that was left over from the example above is what the % (modulo operator) returns.

So we can see now that (any number % 2) that returns a remainder greater than 0 is an odd number.

Further examples: 8 % 2 == 0 (even) 9 % 2 == 1 (odd) 512 % 2 == 0 (even) 777 % 2 == 1 (odd)

1 Answer

'else' indent is not right in your pasted code. Should be like this:

def is_odd num: if num % 2 != 0: returns True else returns False

also to explain %

5 % 2 = 1 .... whereas 1 is the remainder of 5 divided by 2 6 % 2 =0 .... 6 is divisible by 2 with a remainder of 0.

therefore the function above tests a num to see if it returns 0 or something else.

I hope this helps.