python 3.6 - Lists:-Why does 1st for loop in the description ends up infinite loop while 2nd for loop works really well -
for loop 1
li=[] in li: li.append(i**2) print (li) """error...: traceback (most recent call last): file "<ipython-input-29-c20734da477f>", line 2, in <module> li.append(i**2) keyboardinterrupt"""
for loop 2
li=[] in range(0,n): li.append(i**2) print (li)
both should have worked 1st loop goes in infinite loop.why??
the first loop doesn't execute because li
empty.
despite that, in case if li
wasn't empty, e.g li = [1]
, continuously adding elements for in li
have elements iterate through. growing list while going through it.
using range, limit number of iterations specific range. after iterate through range of items, loop stops.
in short, these aren't similar loops.
Comments
Post a Comment