Python Glossary Русский

all Python 3Arithmetic & operatorsterm

Assignment operators

assignment

The augmented assignment operators: +=, -=, *=, /=, //=, %=, **=. They combine an operation with an assignment, updating the variable in place.

Syntax

x op= value  # эквивалент x = x op value

For mutable objects += edits the object in place: lst += [1] is effectively extend, so every other name bound to that same list sees the change, while lst = lst + [1] builds a new list. With int, str and tuples there is nothing to mutate, so you always get a fresh object and the old one simply loses a reference. Python has no ++ or -- : ++x is just two unary pluses that do nothing, and x++ is a syntax error.

Examples

x = 10; x += 5; print(x)  # → 15
x = 10; x -= 3; print(x)  # → 7
x = 4; x *= 3; print(x)  # → 12
x = 10; x /= 4; print(x)  # → 2.5
x = 10; x //= 3; print(x)  # → 3
x = 10; x %= 3; print(x)  # → 1
x = 2; x **= 8; print(x)  # → 256

See also

Open in the full glossarydocs.python.orgReport an inaccuracy