Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loops using 'for' in Python [duplicate]

Why does this not create an infinite loop?

a=5
for i in range(1,a):
  print(i)
  a=a+1

or this

for i in range(1,4):
  print(i)
  i=i-1

or this

for i in range(1,4):
  print(i)
  i=1

Is there any way we can create infinite loops using a for loop? I know there is the while loop for that but I was just curious.

like image 539
Nischal Avatar asked Jan 11 '18 11:01

Nischal


3 Answers

range is a class, and using in like e.g. range(1, a) creates an object of that class. This object is created only once, it is not recreated every iteration of the loop. That's the reason the first example will not result in an infinite loop.

The other two loops are not infinite because, unlike the range object, the loop variable i is recreated (or rather reinitialized) each iteration. The values you assign to i inside the loop will be overwritten as the loop iterates.

like image 135
Some programmer dude Avatar answered Nov 16 '22 01:11

Some programmer dude


Consider a for loop:

for item in iterable:
    print(item)

The idea is that as long as iterable is unchanged, we will loop through each and every item inside iterable once. For example,

for item in [3, 2, 1, 666]:
    print(item)

will output 3 2 1 666. In particular, we find that range(1, 4) is a easy way to represent an iterable [1, 2, 3]. Thus,

for i in range(1, 4):
    print(i)

will output 1 2 3.


Example 1

a=5
for i in range(1,a):
  print(i)
  a=a+1

In this case, range(1,a) is evaluated once, when the loop begins.

Example 2

for i in range(1,4):
  print(i)
  i=i-1

In this case, i is reevaluated every loop, before executing the print and i=i-1 statements within the body of the loop.

Example 3

for i in range(1,4):
  print(i)
  i=1

Just like Example 2, i is reevaluated every loop.

like image 39
Mateen Ulhaq Avatar answered Nov 16 '22 00:11

Mateen Ulhaq


You can't, in this case, update the iterator that your for loop is looping over.


The range in for i in range(a): is actually a function - it takes a value, a, and returns an object that contains the values that it will loop through. Once you've built that object you can change the input variable as much as you'd like, and that object won't change.

Imagine if we made our own similar function called my_range that generates a list (whereas the built in range function generates a range):

def my_range(end):
    my_list = []
    for i in range(end):
        my_list.append(i)
    return my_list

Now if we were to use our new function, like so:

a = 4
for i in my_range(a):
    print(i)
    a += 1

It'd be obvious that we can't update the list object that we're looping over by changing a, because the list that we're looping over has already been made, and isn't being remade on every loop.


Can you make an infinite loop in python? Yes, just add a new entry to the object that you're looping through, e.g.:

my_list = [0]
for i in my_list:
    print(i)
    my_list.append(i+1)

Now we're updating the object that we're looping over.

like image 4
Ari Cooper-Davis Avatar answered Nov 16 '22 02:11

Ari Cooper-Davis