How Range Is Used For Looping?


What if you use a for loop that does the appending in the range (0,5)?
How to even Do that ?


I'll make make a general syntax..


for i in range(0,5):
    #append something in your list


range() is a function that returns an iterator. This is very much like iterating a list.

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

for i in range(5):
    board.append(['O'] * 5)

This will produce a list of lists:

[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]


Thank You Guys :smiley:


For good measure here is an explanation that covers both Python 2 and Python 3 versions of the range function...


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.