Python Lessons – 1.10 In-Place Operators

Python Lessons - 1.10 In-place operators

In-Place Operators

There are some operators that allow you to write numerical expressions in a more contracted form using both the operation and the assignment with a single operator.

That is the expression

>>> x = x + 2

it consists of two operations

  1. sum of the value of the variable contained in x with the value 2
  2. assignment of the result to the same variable

When a reassignment of the result is performed on the same variable, the In-Place operators can be used. (Only in this case)

In this case, in fact, the previous expression can be written

>>> x += 2

The same thing can be done with other operators

>>>x -= 1
>>> x *= 2
>>> x /= 3

Interesting use of strings is interesting. We can use it to make a string concatenation

⇐ Go to Python Lesson 1.9 – Variables

Go to Python Lesson 1.11 – Using an editor ⇒

Leave a Reply