Python - Statement Compound Assignments

Introduction

The following table lists Python augmented assignments.

X += Y    X &= Y      X -= Y      X |= Y 

X *= Y    X ^= Y      X /= Y      X >>= Y 

X %= Y    X <<= Y     X **= Y     X //= Y 

These formats are just shorthand.

They are the combination of a binary expression and an assignment.

For instance, the following two formats are roughly equivalent:

X = X + Y                       # Traditional form 
X += Y                          # Newer augmented form 

Augmented assignment works on any type that supports the implied binary expression.

For example, here are two ways to add 1 to a name:

Demo

x = 1 
x = x + 1                   # Traditional 
print( x )
x += 1                      # Augmented 
print( x )

Result

Related Topic