Answer the question
In order to leave comments, you need to log in
How does this notation “a += 1” work?
I came across such an entry in python "a += 1", but I didn’t really understand the essence. Tried the code, it just gave the same number.
Answer the question
In order to leave comments, you need to log in
like a function in school: y(a) = a + 1, where a is any number, i.e. y(7) = 7 + 1 ==> y = 8
1 is added to what is in "a", that is, if there is 2, then it will be 3; if you loop, 1 will constantly be added until you stop
In your example, " a += 1
" is an increase of a by 1 , which can be written as " а = а + 1
". In other languages, there are increment options such as ++а
or а++
, perhaps you have met them, and if not, now these options will also be clear. Also, before the = sign , you can also find signs of other operations - , / , % , // , * The meaning will be similar for everyone - "Do some action with a and write the result to a ".
" а //= 10
" , for example, will decrease the number a by an order of magnitude. This is a shorthand notation for " а = а // 10
"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question