l
is passed as an argument to range
function whose value is modified inside for
loop, but the loop is going for 10
times
The issue is not how often range
evaluates its argument, but how often for item in sequence
evaluates sequence
. The answer is once. When you write for i in range(l)
, range(l)
is evaluated once and that's it.
range(l) is evaluated once, what is being updated is the value of l in the print statement.
No, the for
loop evaluates the iterable expression just once.
range()
is called once, and the for
loop then iterates over the result.
Quoting from the for statement documentation:
The expression list is evaluated once; it should yield an iterable object.
emphasis mine.