in place operators | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

in place operators

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

10th Sep 2017, 12:44 PM
Mansour
55 Answers
+ 21
the better way to write it is the next x = 5 x += 3 is the same to x = x + 3 print(x)
19th Mar 2021, 11:41 AM
Roberto Williams Hernández Carballo
Roberto Williams Hernández Carballo - avatar
+ 14
x = 2 x += 3 # it's means x = 2 + 3 Print(x)
26th Oct 2021, 6:54 AM
Ahmed Mohammed
Ahmed Mohammed - avatar
+ 8
Please, help, how I can to do the project number 18??? calculator?
7th Apr 2021, 10:16 AM
Меиржан Бегманов
Меиржан Бегманов - avatar
+ 8
X = 2 X+=3(it means X = X +3, X = 2 + 3, X = 5)
2nd Sep 2021, 3:48 PM
Khwairakpam kabita
Khwairakpam kabita - avatar
+ 4
5! = 5*4*3*2= 120
29th Jun 2021, 6:21 PM
Andrew Kuhn
Andrew Kuhn - avatar
+ 4
Please how can i solve this project Write a program to take two integer as input and output their sum also result in strings Sample input 6 3 Sample output 9
8th Mar 2022, 10:16 AM
Honeycrown
+ 4
I'll try to explain it: As these both are same x = x + 3(it will add 3 to x and assign the new value to variable x thus overwrite s it with the new value. It is because python first performs operation on the right side of assignment operator(=) and then initialize the final value to the variable.) Now : x += 3( it will take x on both sides of assignment operator (=) then takes the operational operator(which in this case is +) and performs desired operation on right side first and then initialize it's value to the variable on left side)
7th Apr 2022, 3:52 PM
Umar Farooq
+ 3
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.
10th Sep 2017, 12:50 PM
LordHill
LordHill - avatar
+ 3
X+=3 It simply means X=X+3
14th Apr 2021, 2:45 PM
Mayank Hemnani
Mayank Hemnani - avatar
+ 3
Your code: X=2 X=X+3 print(X) Console: 1)X:2 2)now X:5 3)show X(5) to user
13th May 2021, 2:45 AM
mohammad hosain darijani nasab
mohammad hosain darijani nasab - avatar
+ 3
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
28th May 2021, 3:32 PM
RAKSHITHA M R
+ 3
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
5th Jun 2021, 5:35 AM
Poojitha guntur
Poojitha guntur - avatar
+ 3
Compute X=9 Print (x) X *=3 Print (x)
4th Sep 2021, 9:02 AM
Sir-Richards Gunia
Sir-Richards Gunia - avatar
+ 3
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.
20th Nov 2021, 6:02 AM
samuel bassey
samuel bassey - avatar
+ 3
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.
7th Dec 2021, 9:52 AM
Nana Senne Ackaah Gyasi
Nana Senne Ackaah Gyasi - avatar
+ 3
The first line assigns the value of x to 2. The next line adds on 3 to the existing value, with '+=', and 5 is printed out as that is the current value of x. Similarly, writing X = 2 and then X = X + 3 would give the same value. X should be in lowercase, capital letters are used for variables that do not change.
23rd Feb 2022, 8:58 AM
Annie
Annie - avatar
+ 3
It fist makes the variable X with the number 2 And then it changes it but as you can see there is a + sign so it means it will add the older X variable with the new X variable So 2+3=5 And if you want you can change it from plus to any operation you want
13th Mar 2022, 1:34 PM
Rubain M Riyam
Rubain M Riyam - avatar
+ 2
IN PLACE OPERATORS Inplace operation is an operation that changes the content of a given algebra directly. That is, it changes in a single line without making a copy.
22nd Nov 2022, 9:08 AM
Joseph Chukwudiegwu
Joseph Chukwudiegwu - avatar
+ 1
I dont know how i can message you because i dont understand this
14th Mar 2022, 9:52 AM
Honeycrown
+ 1
Ok so first I will tell you how to message me
15th Mar 2022, 4:02 AM
Rubain M Riyam
Rubain M Riyam - avatar