I cant seem to fine what the operator sign " += " stands for?
+= is an assignment operator It's used as a shorthand in the following type of statement. Instead of writing, a = a +1 (i.e. incrementing the variable by 1) one can write the above in shorthand as: a+=1 (i.e. increment the value of a by and assign that to a)
Thank you,
Also, you can use it with basically any other operation, i.e., a/=2 is equivalent to a = a/2, a *= 2, is a = a*2 etc.
That is very use full information. It helps to shorten the code.
One thing I have done in the past is go to Amazon.com and look at their python books and when they say look inside is look through the index section and many times syntax will show up that way and give a brief description.
This kind of assignment exists in most languages nowadays (java, c, javascript, ...) but you should be warned that it may make your code less readable, especially if you're a new programmer. Don't sacrifice readibility to syntactic sugar !
yes it is, i think this operator is basically from c, then use by the next generation programming language like java, php, and so on. like in java we can use ".=" to concate two words or two variable
actually shorthands might make code more readible, because if you use long variable names which explain purpose of variable then it may take a lot of space to write and your eyes need to go from left to right too much
I agree with you that it helps when you're already experimented. But when someone starts to program, I don't recommend it. It's probably a matter of taste, so try it if you want, and see what's the more readable for you.
owh ok, thanks for your recommendation :)
I must say that I find the ++ notation a lot cuter than += but, alas, I am not the BDFL :-)
actually if you use ++ , usually use at while loop, or for loop
Yeah, I know. I was just saying that I think ++ a lot cuter than += 1, that I find rather ugly for a shorthand.
Also, in the C family of languages, the unary pre/suffix increment/decrement (as well as the compound assignment operators) is a valid expression, whereas in Python the compound assignment operators (+= etc.) must be at the top level of a statement. In C: A[i += 1] = 4; // Alright in Python: A[i += 1] = 4 # BZZT!! SYNTAX ERROR!!!
http://docs.python.org/reference/simple_stmts.html#augmented-assignment-statements
@bmp, the coding style is depend on the person :)
but in python you can't use ++, can you?
i don't know tomas, try to ask another one here, i don't program in phyton yet
it was kinda rhetorical question, because it doesn't allow me to use ++
owh, ok. thanks for sharing
Join our real-time social learning platform and learn together with your friends!