B
B
Brodyachiykot2021-10-22 20:55:35
Python
Brodyachiykot, 2021-10-22 20:55:35

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

5 answer(s)
A
Alexander Karabanov, 2021-10-22
@karabanov

Increment and Decrement operators in
Python

B
BranchInCode, 2021-10-22
@Vetka_in_code

like a function in school: y(a) = a + 1, where a is any number, i.e. y(7) = 7 + 1 ==> y = 8

K
KraGen.Developer, 2021-10-22
@KraGenDeveloper

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

A
Alexey, 2021-10-22
@shamen

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"

M
Maxim Nevzorov, 2021-10-22
@fixator10

Augmented assignment statements

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question