Python’s *in-place bitwise left-shift operator*`x <<= y`

calculates the left-shift operation `x << y`

, and assigns the result to the first operands variable name `x`

. You can set up the in-place left-shift behavior in your own class by overriding the magic “dunder” method `__ilshift__(self, other)`

in your class definition.

>>> x = 8 >>> x <<= 2 >>> x 32

The expression `x <<= y`

is syntactical sugar for the longer-form `x = x << y`

:

>>> x = 8 >>> x = x << 2 >>> x 32

Let’s explore some examples on different data types of the operands.

## Recap Bitwise Left-Shift

The Python ** bitwise left-shift** operator

`x << n`

shifts the binary representation of integer `x`

by `n`

positions to the left. For a positive integer, it inserts a `0`

bit on the right and shifts all remaining bits by one position to the left. For example, if you left-shift the binary representation `0101`

by one position, you’d obtain `01010`

. Semantically, the bitwise left-shift operator `x << n`

is the same as multiplying the integer `x`

with `2**n`

.Here’s a minimal example:

print(8 << 1) # 16 print(8 << 2) # 32 print(-3 << 1) # -6

Feel free to watch my explainer video here:

**Related. **To learn more about the bitwise left-shift operator, and how it works on positive and negative integers, check out our related tutorial: Python Bitwise Left-Shift Operator

## Incompatible Data Type

What if two operands have an incompatible data type—unlike floats and integers? For example, if you try to shift a float variable by a list variable (which doesn’t make sense)?

>>> x = 3.0 >>> y = [1, 2] >>> x << y Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> x << y TypeError: unsupported operand type(s) for <<: 'float' and 'list'

The result of incompatible addition is a `TypeError`

. You can fix it by using only compatible data types for the in-place bitwise left-shift operation.

Can you use the bitwise left-shift operator on custom objects? Yes!

## Python In-Place Bitwise Left-Shift Magic Method

To use the in-place bitwise left-shift operator `<<=`

on custom objects, you need to define the `__ilshift__()`

method (*“dunder method”, “magic method”*) that takes two arguments `self`

and `other`

, updates the first argument `self`

with the result of the operation, and returns the updated object.

In the following code, you combine two `Data`

objects using the in-place left-shift operation:

class Data: def __init__(self, data): self.data = data def __ilshift__(self, other): self.data <<= other.data return self x = Data(8) y = Data(2) x <<= y print(x.data) # 32

You can see that the content of the first operand is updated as a result of the in-place bitwise left-shift operation.

## Python In-Place Operators

In-place assignment operators (also called *compound* assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, `x += 3`

is the same as `x = x + 3`

of first calculating the result of `x +3`

and then assigning it to the variable x.

Operator | Name | Short Example | Equivalent Long Example |
---|---|---|---|

`=` | In-place Assignment | `x = 3` | |

`+=` | In-place Addition | `x += 3` | `x = x + 3` |

`-=` | In-place Subtraction | `x -= 3` | `x = x - 3` |

`*=` | In-place Multiplication | `x *= 3` | `x = x * 3` |

`/=` | In-place Division | `x /= 3` | `x = x / 3` |

`%=` | In-place Modulo | `x %= 3` | `x = x % 3` |

`//=` | In-place Integer Division | `x //= 3` | `x = x // 3` |

`**=` | In-place Power | `x **= 3` | `x = x ** 3` |

`&=` | In-place Bitwise And | `x &= 3` | `x = x & 3` |

`|=` | In-place Bitwise Or | `x |= 3` | `x = x | 3` |

`^=` | In-place Bitwise XOR | `x ^= 3` | `x = x ^ 3` |

`>>=` | In-place Bitwise Shift Right | `x >>= 3` | `x = x >> 3` |

<<= | In-place Bitwise Shift Left | `x <<= 5` | `x = x << 5` |

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.