Let's first understand what range
actually does. range
, in its most basic version, takes a single integer as its parameter (in your case, x
). It gives a list of integers (in Python 2) as the output (list
in English is pretty much the same as list
in python, but I'm not going into details here). The list contains numbers starting from 0 to the parameter, 0 included and the parameter excluded.
So, range(5)
gives [0, 1, 2, 3, 4]
Now, your loop effectively becomes:
for i in [0, 1, 2, 3, ..., x]:
When x
is 0, then the list can't include 0 either. So you get the following:
for i in []
Since there are no values i
can take, the loop doesn't run!
Since you define result = 1
before the loop, return result
simply spits out the same value if the loop isn't executed.
In Python 3, the range()
function gives a generator
object, similar to the xrange()
function in Python 2.