Q. What is the difference between, a + 1 and a += 1 and a = a + 1? To find out, execute the following code fragments and write your answer below:


Fragment 1


a = 20
a + 1
print (a)

Fragment 2


a = 20
a += 1
print (a)

Fragment 3


a = 20
a = a + 1
print ()

Answer :-

Output fragment 1:- 21
But value of variable ‘a’ will not change.

Output fragment 2: -21
But value of variable ‘a’ will change. Now new value of variable ‘a’ is 21.

Output fragment 3: -It will print nothing because print statement is empty.
But value of variable ‘a’ will change. Now new value of variable ‘a’ is 21.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post