in place operators | Sololearn: Learn to code for FREE!

+8

in place operators

I do not understand how this code work? X=2 X+=3 print (X) The answer is 5!

9/10/2017 12:44:27 PM

Mansour

31 Answers

New Answer

+10

Please, help, how I can to do the project number 18??? calculator?

+6

x = 2 x += 3 # it's means x = 2 + 3 Print(x)

+4

the better way to write it is the next x = 5 x += 3 is the same to x = x + 3 print(x)

+4

5! = 5*4*3*2= 120

+4

X = 2 X+=3(it means X = X +3, X = 2 + 3, X = 5)

+2

This operator is called as compound assignment operator , it increases the readability of the code. This operator adds the value with the variable which is on the LHS which is nothing but x = x +3

+1

X=2 is assigning the value of 2 to the variable X.. The next line is adding 3 to the variable X. Since X is = 2, adding 3 to it makes 5.

+1

X+=3 It simply means X=X+3

+1

Here first the value of X is assigning with the value 2. The next line it is in assignment operator (eg:X+=2 i.e X=X+2) so,X is 2 ,adding 3 to it so the answer is 5 then print the X valuee

+1

X += 3 Is the same as X = X + 3 Same goes for other operators Eg. x %= 3 ( same as x = x % 3) We already know that x = 2 So using x += 3 will give same output as x = x + 3 Which is x = 2 + 3 = 5.

+1

I hope that my explanation would help here. So the in-place operators makes your code more simple, short and faster when executed. So for example >>> x = 1 print (x) #This gives us a value of 1. Assuming we want to increase the value of x by any integer n. We would normally write: x = x + n. If our integer is 1, we would have x = x + 1 which is the same as x+= 1. + means increment, and the increment is by the value after the '=' sign. We can increase, decrease, multiply and divide by any integer we want. And lastly, the value of x becomes the new value after x was increased in our example. So the new value of x becomes 2.

0

This is a very strange way of writing.

0

Thank you

0

x+3=5 is also we right x+=3

0

Your code: X=2 X=X+3 print(X) Console: 1)X:2 2)now X:5 3)show X(5) to user

0

X takes value 2 and it us added to 3 which gives 5(2+3)

0

The first assignment of x is 2 then the second assignment of x take 2 from the first assignment of x and then adds 3 to it meaning that 2+3=5

0

Here, initially X is assigned with 2 i.e (X=2) According to assignment operators The expression X+=3 is equal to X=X+3 substitute x=2 which is initially assigned Now X= 2+3 = 5 Hope that this will help :)

0

Given code X = 5; X+=3; We know X = 5; Then (X = X+3) So print(x)

0

i understand tnx all