1

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

2 Answers 2

1

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)).

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks, do check enumerate(), for i,v in enumerate(a)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.