+ 1
Can someone please explain Assignment Operators/ In- Place Operators?!
I am completely confused on Assignment Operators ( Ex. X+=3) Can someone explain this as easily and understandable as possible? Here is a link to the lesson: https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2429/. Any and all explanations welcome!
4 Respuestas
+ 5
It is usually a reduced form of raising an expression. But it is better to see it with an example:
# Declaration and assignment of a numeric variable.
my_number = 5
# The next line is equivalent to saying something like "The value that is stored in the variable my_number plus 2"
my_number = my_number + 2 # 5 + 2 = 7
This can be simplified by writing it in the following way:
my_number + = 2 # 5 + 2 = 7
In the same way this works with the operations of addition, subtraction, multiplication and modulation of a division (- =, * =, / =,% =).
In the same way with the variables type String can be used, but with a different behavior:
# Declaration and assignment of a variable type string.
my_string = "hello"
# The following line is equivalent to saying "To the content that is stored in the variable my_string adds the string 'world' to the end"
my_string = my_string + "world"
#This is equivalent to my_string = "hello" + "world"
And in the same way it can be simplified to:
my_string + = "world"
The strings can also be used like this:
my_string = my_string * 2 #my_string = "hellohello"
That is equivalent to saying "Repeat the code that is stored within the variable my_string x times" (in this case twice). And in the same way it can be reduced to:
my_string * = 2
I hope my explanation does not cause you more confusion lol
+ 4
Yeah. Keep in mind that the first example should be x = x + 8
+ 1
Thank you! I got it now!
0
So it is a basic variable just rearranged?
Example: x=7
x+8. (=15)
but instead: x=7
x+=8
(still amounts to 15)