I've been writing a program to pick random words from my list. However, to do that I had to imitate some solutions on the internet, and I succeeded. Unfortunately, there is something that I can't understand in my work.

def repeat(pic_word, n):
    for i in range(n):

My question is what is the meaning of i in "for i in range"?

  • Note that in the example you've shown i isn't used, and is somewhat meaningless - it's therefore conventional to use for _ in range(...): to indicate this. – jonrsharpe Nov 12 '15 at 15:20

i is just the name of a variable, in this case an integer. Most programmers will use i for the outermost for loop when iterating through a list of numbers.

What this loop is doing is creating a range from 0 to n (some number). Then it's looking at all the numbers in there, one at a time. In this case the loop is running pic_word() n times.

Not the answer you're looking for? Browse other questions tagged or ask your own question.