1
a=2
for a in range (9) :
print (a+1)
Why using the for
, in this case, it is printed from 1 to 9, and the variable a
is equal to 2?
1
a=2
for a in range (9) :
print (a+1)
Why using the for
, in this case, it is printed from 1 to 9, and the variable a
is equal to 2?
6
No, the variable a
within the loop is not worth 2. Although the syntax is not so explicit, the loop used is creating an assignment, so at each step the value of a
changes, including the first time. The range()
generating the values that will be assigned to the variable a
, which is exactly what this form of for
proposes to do. The variable there is to be assigned.
Think of him like that:
for a = range(9):
With each passage will come a new element of the desired track.
If you wanted to keep the value of a
then the loop variable should be another. If you wanted the track to start at 2 then you should do:
for a in range(2, 9):
print (a + 1)
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Browser other questions tagged python variables for
You are not signed in. Login or sign up in order to post.
The translation of this line would be something like for each "a" in the range up to 9 make:
– Bacco