All Courses

For loop giving incorrect answer

By Lekshmi309, 2 years ago
  • Bookmark
0

for i in range(1,11): print(i) i=i+1

in the for loop i have added an updation for variable 'i' inside the loop instead of giving step size as 2. But the value of 'i' doesnt get updated.why its so??

Loop
3 Answers
0
Ashiqmukkil*

After executing your code it prints the numbers from 1 to 10 which is the expected output and the value of i is getting updated.


0
Lekshmi309

yes. its get updated while using range function with step size 1.Bt i have given another updation for the same variable 'i' inside the loop.ie first value of i is 1 and it gets printed,next inside the loop the same variable is updated ie 2.and nextly it should get updated using range function ryt??

it is because that in range function it re-initialize the value in his sequence. It does not considers the variable i inside the loop. for i in range(1,10) it will iterate from 1 to 10 and for each iteration it will allocate next value of range in i.

– maheshpardeshi May 8, 2021 at 8:32 PM

0
Lekshmi309

k..thank u

Your Answer

Webinars

Why You Should Learn Data Science in 2022?

Dec 15th (7:00 PM) 277 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More