Does Python have augmented assignment statements corresponding to its boolean operators?
For example I can write this:
x = x + 1
or this:
x += 1
Is there something I can write in place of this:
x = x and y
To avoid writing "x" twice?
Note that I'm aware of statements using &= , but I was looking for a statement that would work when y is any type, not just when y is a boolean.
The equivalent expression is &=
for and
and |=
for or
.
>>> b = True
>>> b &= False
>>> b
False
Note bitwise AND
and bitwise OR
and will only work (as you expect) for bool
types. bitwise AND
is different than logical AND
for other types, such as numeric
>>> bool(12) and bool(5) # logical AND
True>>> 12 & 5 # bitwise AND
4
Please see this post for a more thorough discussion of bitwise vs logical operations in this context.