+ 1
for loop Quiz😇😇
def f(x): for i in range(x): print(i) x+=1 f(3) will print 0,1,2. Why range(x) never change? Can We change the value of x in range(x),how?😂
2 Respostas
+ 9
In the function 'f', 'x' refer to the argument passed.
You can change its value inside the function, but the 'x' reference used as argument of the range() function called is only called once: when initializing the loop, the range() function return a list of values through which the loop will iterate... So, the code of your example is equivalent to:
def f(x):
r = range(x) # with x==3, r==[0,1,2]
for i in r: # will sucessively assign 0, 1 and finally 2 to 'i' (the items of the list, without use of 'x' value)
# ...