How can I check if a number is odd?


#1

Question

How can I check if a number is odd?

Answer

A number is odd if it is not evenly divisible by 2, which means we should make use of the modulo % operator. The modulo operator returns the remainder after devision, so if something is evenly divisible by 2, with a remainder of 0, it isn’t odd!
if my_number % 2 == 0: means a number is even.


FAQ: Learn Python - Practice Makes Perfect - purify
#2

QQ%E6%88%AA%E5%9B%BE20181017164414
Hi all, Could u tell me why my funtion will be fail? and it can’t remove num 5 in list


#3

.remove() is not the right approach to solve this problem, removing from the same list as you are looping over causes undesired behavior, like skipping successive odd/uneven numbers.


#4

remove is the wrong approach because remove alters the length of the list and the index value of items further down the list of the item removed.