Simple for loop question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 '=' ?

27th May 2017, 7:20 AM
hahaha
hahaha - avatar
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????
27th May 2017, 7:41 AM
Mrinmoy Kalita
Mrinmoy Kalita - avatar
+ 2
if we don't add '=' , we cannot add mysum to i That the area I am confusing
27th May 2017, 7:55 AM
hahaha
hahaha - avatar
+ 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
27th May 2017, 8:23 AM
hahaha
hahaha - avatar
+ 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..
27th May 2017, 8:29 AM
Mrinmoy Kalita
Mrinmoy Kalita - avatar
+ 1
Sorry but why mysum+= i is equivalent mysum=mysum+1??
27th May 2017, 7:48 AM
hahaha
hahaha - avatar
+ 1
not 1 sorry i
27th May 2017, 7:49 AM
Mrinmoy Kalita
Mrinmoy Kalita - avatar
+ 1
Oh I try to change mysum+=i to mysum=mysum+ i And it works!!
27th May 2017, 8:02 AM
hahaha
hahaha - avatar
+ 1
That meaning there is an operator '+='??
27th May 2017, 8:03 AM
hahaha
hahaha - avatar
+ 1
what
27th May 2017, 8:03 AM
Mrinmoy Kalita
Mrinmoy Kalita - avatar
+ 1
Sorry I could not understand the purpose of += here
27th May 2017, 8:18 AM
hahaha
hahaha - avatar