Python i Meaning In For Loop

Python provides the for loop in order to iterate over multiple items. There may be different types of items like names, dictionaries, and numbers. The Python i is a popular variable name used in “for loops”. “Python i” is mostly used to iterate over a list of numbers and these numbers can be also generated with the range() method. The “Python i” name comes from integer where the i is used to express integer.

Python i Loop

The Python loop syntax is like below where every iterated item is stored inside a variable which is named as i for this tutorial.

for i in ITERABLE:
  • The i is the current step value which is assigned from the ITERABLE
  • ITERABLE is a list that contains numbers or generators like range().
  • CODE is executed for every iteration where the i variable is generally used.

Iterate i with List

In the following example, we iterate over a list of integers or numbers. We assign one item from the list to the variable i and then use the variable i inside the for loop body.

for i in [1,2,3,4,5]:
   r = i+i

Iterate i with Range

We can use the i variable with for loop by using the range() method. We use the range method to generate a list of numbers that starts from 0 and stops at 5.

for i in range(5):
   r = i+i

Iterate i with Start, Stop

We can use the range() method with i variable by specifying the start and end numbers.

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

Iterate i with Start, Stop and Step

Alternatively, we can specify the start, stop and step values for the range() method while using the i variable.

for i in range(1,5,2):
   r = i+i

Iterate i Negatively

We can iterate negatively by using the range() method. We specify the step as a negative value like “-1” and also provide the start and stop numbers.

for i in range(5,1,-1):
   r = i+i

Leave a Comment