range(x)
returns a list of numbers from 0 to x - 1.
>>> range(1)
[0]
>>> range(2)
[0, 1]
>>> range(3)
[0, 1, 2]
>>> range(4)
[0, 1, 2, 3]
for i in range(x):
executes the body (which is print i
in your first example) once for each element in the list returned by range()
.
i
is used inside the body to refer to the “current” item of the list.
In that case, i
refers to an integer, but it could be of any type, depending on the objet on which you loop.