Saturday 15 May 2010

python - List comprehension and % function -


When I am using the following code

  9, 10, 2) If not, then I% 3)]   

gives it

  & gt; (-9, -3, 3, 9)   

Why do you give this answer? What does 'i% 3' mean?

Thank you.

In your example, % is a% b left a / b

in your example, goes through the loop:

  [- 9, -7 , -5, -3, -1, 1, 3, 5, 7, 9]   

-9% 3 is equal to 0, because -9 3 is divisible by 3, and thus there is no remainder, however, 5% 3 2 returns because 3 in 5 goes once, and 5 - 3 == 2 .

No I% 3 is a bit difficult first, i% 3 is evaluated. If the result is greater than 0, then it is considered as true . No is a Boolean operator which becomes negative for bullion, then is not true is wrong (and is not wrong ).

0 is is incorrect , so if i% 3 == 0 , then i < / Code> will be included in the new list. > "Hello World" print because of the modulo operator is not. This is what the document will explain to me better: P It just happens that % is also used.

No comments:

Post a Comment