What is a counter variable in a Python loop?

In languages ​​where loops are forbuilt similarly to this:

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

the variable iin this example usually refers to as a "counter variable".

What is the equivalent technical name for a loop variable in Python, where loops are formore like for each?

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

Here, the "counter variable" seems much less obvious.

0
source share
4 answers

First of all, Python loops are fornot like C loops for. There is no counter because the Python construct is for every construct . Also see Python Tutorial :

for Python , C Pascal. , (, Pascal) , ( C), Pythons for (a ), .

iterable , . ; Python .

for:

,

, . , , ..

+4

- "placeholder variable/s", ", , for".

, @Ajax1234, , , , , for.

, "placeholder" - , :

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}')

:

a = a
a
b
c
d
a = d

, - , a 'a', 'b', 'c', 'd'.

+1

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

i , , / .

Python , .

i :

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

, .

i , :

, for , StopIteration, for .

:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

; . expression_list. , , , . , , , (. ), . ( , StopIteration), else, , , .

: target_list , .

0

I have always used the term "controlled variable" because the loop controls its meaning. Programmers in most languages ​​seem to understand this, although I can hardly say that this is an industry standard.

0
source

Source: https://habr.com/ru/post/1686639/


All Articles