0

I'm trying to figure out why the list comprehension below returns what it returns. I get a little confuse with the modulo indicator.

Example: The output below i cant understand the output.

print([i for i in range(11) if i%2])

Output: [1, 3, 5, 7, 9]

However in the example below I perfectly understand the return of even numbers.

print([i for i in range(11) if i%2==0])

[0, 2, 4, 6, 8, 10]

Ian_De_Oliveira
  • 291
  • 5
  • 16
  • not really answered in the linked "duplicate"! the first one is giving you uneven numbers because you ask if there is a modulo by doing if i%2 - in the second one, you ask if the modulo is 0 and therefore getting even numbers back. – Cut7er Aug 28 '18 at 13:05
  • @Cut7er , what you mean by "if that is a modulo %2? – Ian_De_Oliveira Aug 28 '18 at 23:45
  • if you do `if i%2 == 0`, you check if the remainder (or modulo) is zero. by doing `if i%2`, you check if there is a remainder. that's why both these give even or uneven results back! – Cut7er Aug 29 '18 at 06:48

0 Answers0