if it wasn’t for the range…

So, let’s combine some of what we’ve learnt from the past few tutorials. Let’s say we wanted to print all the even numbers from 0 to 20. We know how to get these numbers by using range(21), but this gives us all the numbers from 0 to 20. We just want the even ones.

Even numbers are those which have a remainder of 0 when you divide them by two. Python has a special operator (called %) which does just this:

>>> 1%4
1
>>> 2%4
2
>>> 3%4
3
>>> 4%4
0
>>> 5%4
1

So for example: 1%4 means “what is the remainder when you divide 1 by 4” (it’s 1). To test whether a number is even we can see whether this remainder is 0 when the number is divided by 2: