+ 3
Simple for loop question
mysum=0 for i in range(5,11,2) mysum +=i if mysum==5: break mysum +=1 print(mysum) In line 3, when I remove = operator , the result would be zero Why we need to add '=' ?
10 Answers
+ 3
Because mysum+=i is equivelent to mysum=mysum+i..
so if u do not assign mysum+i value to mysum variable..the result would show you zero.. did u understand????
+ 2
if we don't add '=' , we cannot add mysum to i
That the area I am confusing
+ 2
I just found the answer.thank you!!
If I have a '/' instead of '+', the left side element would be divided by right side element.
That mean when I have a single element on left-hand side, I replace that single element with whatever's on the right-hand side
+ 2
not only / operator but will work * , - n % too..
= operator assigns value so if u write mysum+i there will b no logic ..we want there = to assign the calculation to mysum variable..
+ 1
Sorry but why mysum+= i is equivalent mysum=mysum+1??
+ 1
not 1 sorry i
+ 1
Oh I try to change mysum+=i to mysum=mysum+ i
And it works!!
+ 1
That meaning there is an operator '+='??
+ 1
what
+ 1
Sorry
I could not understand the purpose of += here