Python for Programmers: Augmented and Chained Assignment
Welcome to the Augmented and Chained Assignment lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
Expressions like
x = x + 1are common, so Python has a dedicated "augmented assignment" operator.name += valuedoes the same thing asname = name + value.>
x = 3x += 5xResult:
8
There are also equivalents for other mathematical operators:
*=,-=, and/=. However, Python doesn't have thex++operator, which adds 1 toxin many other languages. Trying to use it raises aSyntaxErrorexception.(You can type
errorwhen a code example will cause an error.)>
x = 3x++xResult:
SyntaxError: invalid syntax (<string>, line 2)
We can also assign the same value to multiple variables in one line. This convenient feature is called "chained assignment."
>
x = y = 3x += 1y += 2x + yResult:
9