How can I check if a number is odd?

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.

2 Likes

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

.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.

1 Like

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.

1 Like

When is it useful to use an empty list?
res =

def purify(pur):
  numbers = int()  
  for char in pur:
    if char % 2 == 0:
      numbers += char
    return numbers
  
  
print purify([2,4,6,8])

Any clues?

…got it.

def purify(pur):
  numbers = []
  for char in pur:
    if char % 2 == 0:
      numbers.append(char)
  return numbers
  
  
print purify([4,6,8])

vs.

The first one will work if we cast it to a list.

numbers += list(char)

or

numbers += [char]
3 Likes

I just tried the first method of casting the item to a list, but it’s coming up with an error:

def purify(numbers):
  lista = []
  for n in numbers:
    if n % 2 == 0:
      lista += list(n)
  return lista
print purify([2, 3, 5, 7, 10])

integers can’t be casted to list.

logic variable naming is important, char seemed to indicate a character, in which case you can use list(), which i why i think mtf included list(char) in his answer, but only mtf himself can answer that

This is how we learn about exceptions and errors (by doing it)

>>> list(1)
Traceback (most recent call last):
  File "<pyshell#349>", line 1, in <module>
    list(1)
TypeError: 'int' object is not iterable
>>> a = []
>>> a += [1]
>>> a
[1]
>>> 
1 Like

Hi, you can use “.remove()” if you clone the list and use the clone to loop. Here’s an example code:

def purify(numbers):
–new = list(numbers)
–for num in numbers:
----if num % 2 != 0:
-------new.remove(num)
–return new

print purify([4,5,3,7,7,4])

For this question, why doesn’t the following code work?

def purify(lst):
new_list = lst
for i in lst:
if i % 2 != 0:
new_list.remove(i)
return new_list

mutating a list where you are looping over is a recipe for disaster,

lists can’t have empty spaces/slots, so when you remove an item everything to the right of the removed elements moves one to the left, combined with the loop (which goes to the next index), values will get skipped

Why didn’t my code worked?

#my code:
def purify(numbers):
  result= [ ]
  for i in numbers:
    if i % 2 != 0:
      numbers.remove(i)
      return result

print purify([1, 2, 3, 4])

What I get is an empty , oh why?

here:

result= [ ]

you declare an empty list, then when a number is odd, you remove the element from numbers list and then return the result list (which is empty).

instead of using .remove() you could use .append() for the desired value in a new list.

def purify(lnums):
even_list =
for number in lnums:
if number % 2 == 0:
even_list.append(number)
return even_list

print purify([1, 2, 2, 3, 4, 5, 5, 6, 7])

this would output >> [2, 2, 4, 6]

1 Like

why can you .append() an integer but you can’t .remove() it? if you use .remove() in a for loop i mean yes, it will remove that value at the index and all the other value at the indices will move left and values will be skipped, but since it’s a for loop wouldn’t it re-iterate through it again until the condition is fully met? or is that ability acquired in a while loop?

the loop keeps track of the current position/index. So not possible. So the loop will go from the first element, to the second element and so forth. Not caring about the shifts happening in the list

1 Like

what do you mean current index, the index you input? what is specifically happening in the loop that generates that error when you use .remove() in the loop; and when you say not caring about the shifts happening in the list, is that indicating it won’t iterate through the list more than once to be able to get every value intended, because for that to happen your return would have to be inside the for loop…so consider my question with everything being correctly where it’s supposed to be, and i’m using .remove()

1 Like