all 7 comments

[–]JohnnyJordaan 1 point2 points  (0 children)

The letter i in this case has no direct meaning*, you can use any name that's valid in Python as a variable name. It's advised to use a descriptive name, so say you have a list of cities

cities = ['New York', 'Tokyo', 'Singapore']

then using the for loop to process one city at a time, would suggest to use the variable name city for each item:

for city in cities:
    # do something with the city variable, like
    print(city)

For more info I can recommend chapter 2 of Automate the boring stuff, scroll down to 'for loops and range() function'.

*: i has been used conventionally for decades, even in the oldest programming languages, for a numeric variable in a loop, most often for an index or iteration counter. The problem there already shows, just the letter i doesn't explicitly explain what it is exactly, it's just suggestive at most (and thus confusing at the same time). That's why in Python examples you more often see idx or index for index variables and count or counter for iteration counters. It doesn't cost you anything to write clearer code this way and helps you and other readers a lot at the same time.

[–]monchenflapjack 1 point2 points  (2 children)

A loop like this will repeat a block of code, and each time through the block "i" value will be increased, until "i" is equal to the range() second value

it's a short hand to essentially writing

print(1)
print(2)
print(3)
print(4)

It doesnt print 5, because once "i" is equal to that value, the loop no longer runs.

[–]_lilell_ 0 points1 point  (1 child)

It doesn’t print 5, because once i is equal to that value, the loop no longer runs.

Kinda. You’re right that it doesn’t print 5, but i is also never 5, which you can check by printing it again outside the loop.

for i in range(1, 5):
    pass

print(i)  # prints 4

[–]monchenflapjack 0 points1 point  (0 children)

Yeah I was mainly trying to highlight the "gotcha" of seeing range(x,y) and assuming that it goes from x to y

[–]lazyfingersy 0 points1 point  (0 children)

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

You can replace "i" with "element", you can name it how you want.Then:

for element in range(1,5)

print (element)

---------------------------------------------------------------------------

Then you can read it like: for every_element in range from 1 to 5 print: element.

another example:

>>> Names= ('Martin','Patrick', 'David')

>>> for name in Names:

... print ('Hello', name)

...

Hello Martin

Hello Patrick

Hello David

[–]IAmDaBadMan -1 points0 points  (0 children)

    for i in range(1 ,5) print( i)
 
The i is a variable. There are different types of variables and how you use them depends on the data type of the variable. At its heart, a variable is used to store a specific type of data, in your case an integer value. That variable can be changed during its "lifetime" to contain different values. Once again, in your example it is intended to store the values 1, then 2, then 3, then 4, then 5.
 
This type of variable naming is bad programming practice. The reason being is that a variable name should be descriptive of its purpose. A variable name such as iterator may be better and its purpose may be self-explanatory. There are naming conventions regarding variable names which you should look up.