Why is the in for loop variable not changing?

Why range(len(whole)/2)

doesn't the value change after it's whole

changed? And what do you call the value range(len...)

in the for-loop?

whole = 'selenium'
for i in range(len(whole)/2):
    print whole
    whole = whole[1:-1]

      

output:

selenium
eleniu
leni
en

      

+3


source to share


2 answers


range()

prints a list of integers once. This list is then looped over for

. It is not recreated every iteration; that would be very inefficient.

You can use a loop instead while

:



i = 0
while i < (len(whole) / 2):
    print whole
    whole = whole[1:-1]
    i += 1

      

the condition is while

checked at each iteration of the loop.

+8


source


Range function creates a list

[0, 1, 2, 3]

      

And the for loop iterates over the value of the list.



The list is not recreated every time

But that's not the case on the regular list

wq=[1,2,3]

for i in wq:
    if 3 in wq:
        wq.remove(3)
    print i

1
2

      

+3


source







All Articles