4. Building LIsts


#1

Please help me understand what the % means in

evens_to_50 = [i for i in range(51) if i % 2 == 0]


#2

In python the % is called a modulus. It's function is remainder division, simple as that.

5 % 2 = remainder of 1


#3

The character % means the remainder whole numbers

Example
4 % 2 -> results 0
6%4 -> results 2
10%3 -> results 1


#4

Hi, @dbprojectcomcast_net ,

As @zeziba and @textpro07281 have noted, %, in this context, is a modulus (remainder) operator. With this list comprehension, we want only even numbers to be included in the resulting list. Even numbers all give a remainder of 0 when the divisor is 2, as it is here. The other numbers, which result in a remainder of 1, are odd, and are not to be included in the list. The condition, if i % 2 == 0, in the list comprehension, makes sure that only the even numbers in the range are placed in the list.