Have you noticed this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Have you noticed this?

x=2 print(2) output-->2 print(x+=2) output-->(2+2)=4 now x is 4... Print(x+2) output-->6 this process(x+=a number) rendees the result to be x.. now, x=2 print(x) output-->2 print(x+2) output-->4 here x still remains 2 print(x+3) output-->(2+3)=5 so note the difference between (x+=y) and (x+y)..

24th Jul 2017, 7:03 AM
Otumian Empire
Otumian Empire - avatar
2 Answers
+ 5
The difference between this is crystal clear. += is an assignment operator (x+=2 same as x=x+2) but + is just a simple addition operator. Simply, x=x+2 (or x+=2) is a STATEMENT which PERFORMS AN ACTION while x+2 alone is an EXPRESSION which simply EVALUATES TO A VALUE 😊
24th Jul 2017, 7:14 AM
Hanif Ali
Hanif Ali - avatar
+ 3
yes, the "+=" operator is different with just "+".. x += y is equal x = x + y, so the value of x, will be replace with the result of the operation.. and this applies to subtraction, multiplication, etc....
24th Jul 2017, 8:22 AM
Mugfirfauzy Siregar
Mugfirfauzy Siregar - avatar