0

Isn't it wrong to write x=x+4 using in place operation because on solving x-x=4 which will give 0=4 which is wrong

23rd Oct 2020, 6:23 AM
Ujjwal Sharma
6 odpowiedzi
+ 1
When you write x=x+4 This means you are assigning x a new value of x + 4 Example:- x = 3 #x is assigned to value of 3 x = x+4 #Now x is assigned a value of x+4 you are not saying that x is equal to x+4 you are just saying that give x now a value of x +4 which will be 3+4 print(x) #This will output 7(3+4) If you want to compare you have to use double equal sign print(x==x+4) #This will output False So, = is used to assign value == Is used to compare value Hope It Helps You 😊
23rd Oct 2020, 7:28 AM
Hacker Badshah
Hacker Badshah - avatar
+ 6
At first, saying x = x + 2 looks like a mistake, because logically, if x = x + 2, then 2 = x - x, i.e. 2 = 0, which is impossible. The answer is that in Python, "=" is an assignment operator. x = 5 should be thought of as "x is set at the value 5", rather than "x equals 5", so it's quite ok to say: x = 5 # x is set at 5 x = 6 # now x is set at 6 (and is no longer 5) so "x += 2" means (the new value of x) is set at (the current value of x) + 2" so x = 3 x += 5 print(x) prints 8 because x = 3 + 5 = 8 To say "x equals 3" we use "x == 3", which comes later under Boolean operators. Hope this helps.
23rd Oct 2020, 8:24 AM
David Ashton
David Ashton - avatar
+ 1
In programming x=x+4 is an assignment not an equation as is in math.
23rd Oct 2020, 6:25 AM
Qasem
+ 1
David Ashton , Roman and hacker badshah thank you your answers are amazing
23rd Oct 2020, 9:37 AM
Ujjwal Sharma
0
Ok but what does an assignment mean
23rd Oct 2020, 6:26 AM
Ujjwal Sharma
0
Think about the expression written right of "=" as a function, returning some value (in this case, number). Using "=" you assign this value to variable "x". So, this function just gets value of x (declared before), sums it with 4 and assigns the result to x. In fact, x is just a piece of memory with some data written there, function copies the data, makes operation and puts the result in some piece of memory. In your case, it puts the result back to x, thus "rewriting" it.
23rd Oct 2020, 9:34 AM
Roman