Skip to content Skip to sidebar Skip to footer

What Is The Counter Variable In A Python Loop Called?

In languages where for loops are constructed similarly to this: for (int i = 0; i < max; i++) {} the variable i in this example seems generally to be referred to as a 'counter

Solution 1:

First of all, Python for loops are not like C for loops. There is no counter, because the Python construct is a For each construct. Also see the Python tutorial:

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

The construct takes an iterable actually, and keeps looping until the end of the iterator is reached. It lets you specify one or more names to assign the next object from the iterator to; the Python documentation calls this the target list.

See the for statement documentation:

Each item in turn is assigned to the target list using the standard rules for assignments

Because the target list is assigned elements from the iterable, you could also refer to the variable in context of the iterable. For a list, you can talk about the list element, for looping over a file, the target is assigned the next line, etc.


Solution 2:

I would call it something along the lines of a "placeholder variable/s" as in "the variable that is a placeholder for the items iterated over by the for loop".

It could, as @Ajax1234 pointed out, be a placeholder for a list element, or a dictionary element, a file, almost object that could be in a selection of items iterated over in a for loop.

It could also be noted that in using the word 'placeholder' I'm being rather literal - it can be just about anything you want, eg:

a = 'a'
b = 'b'
c = 'c'
d = 'd'

print(f'a = {a}')
list2 = [a, b, c, d]
for a in list2:
    print(a)
print(f'a = {a}')

Outputs:

a = a
a
b
c
d
a = d

Note that that placeholder variable actually holds the last object iterated over, in this case a is reassigned the strings 'a','b','c','d'.


Solution 3:

In this example

for (int i = 0; i < max; i++) {}

The i is used to help determine when a loop should end/stop iteration.

Python does this automagically, behind the scenes for you.

The variable i in this example:

for i in ('a', 'b', 'c'):

is a placeholder to hold the values that are being iterated over.

It is customary for the i variable to be called by names such as:

  • target variable
  • iteration variable

Behind the scenes, when the for loop runs out of items to iterate over, a StopIteration condition is raised and the for loop will exit.

The documentation references this:

for_stmt ::=  “for” target_list “in” expression_list “:” suite
              [“else” “:” suite]

The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see Assignment statements), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a StopIteration exception), the suite in the else clause, if present, is executed, and the loop terminates.

NOTE: target_list does NOT mean the item is an actual list.


Solution 4:

I've always used the term "controlled variable", since the loop controls its value. Programmers of most languages seem to understand this, though I can hardly claim it's an industry standard.


Post a Comment for "What Is The Counter Variable In A Python Loop Called?"