Sudhir
Sudhir

Reputation: 25

Python For loop Iteran

How can I change value of a variable which iterating over?

for i in range(10,30):
 print('jdvcjhc')
I want to change I value like i do in while loop

i=1`enter code here`
while i<=10
   print('dfhs')

Upvotes: 1

Views: 41

Answers (2)

ravellin
ravellin

Reputation: 26

The range function returns a pre-calculated list of values, created from the input parameters, in this case - (10, 30). Unlike while loop, i here is a 'dummy' variable used as a placeholder for the values read from the list. Within a particular iteration, one can manipulate i as much as they like, but it will inevitably become overwritten with the subsequent list value upon calling the next iteration.

Upvotes: 0

The Shend
The Shend

Reputation: 66

You can't set conditional statements like (i<=10) in For loops. you just can set the range that "i" can change. Actually, if you need a condition for your loop you need to use While or For and If together.

That's my opinion. Regards, Shend

Edit: if you need to change the steps of adding or subtracting "i" you can use step in range function. ( range(start,stop,step)).

Upvotes: 1

Related Questions