When assigning a value to a variable, we often use the variable itself, as in increment:

x = 1
x += 1
print(x)
Copy the code

Running results:

2

In addition to +, Python also supports the following advanced assignment operators:

  • - =
  • * =
  • / =
  • % =
High-level assignment statements Equivalent statements
x += 1 x = x + 1
x -= 1 x = x – 1
x *= 1 x = x * 1
x /= 1 x = x / 1
x %= 1 x = x % 1

In addition to assignment, the += operator can also concatenate strings and lists; The *= operator copies strings and lists. This has been covered in previous articles.


Unfortunately, the Python language does not support the classic ++ and — operations.