Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Understanding the Python **= Operator

Learn how Python's **= augmented assignment operator works with numeric types, how it evaluates targets once, and how to avoid common edge cases.

~5 min readPython
#augmented assignment#exponentiation#python operators#syntax
Illustration of the Python augmented exponentiation operator **= showing a variable being raised to a power and assigned back.

The **= operator in Python is an augmented assignment operator that raises a variable to a power and assigns the result back to the same variable. In Python, x **= y is equivalent to x = x ** y when x is a plain variable name. It is part of the family of augmented assignment operators that include +=, -=, *=, and others. This operator reduces repetition and makes the intent clearer when you want to update a variable by exponentiating it.

What the **= Operator Does

The **= operator performs exponentiation and assignment in a single step. For example:

python
x = 2 x **= 3 print(x) # Output: 8

Here, x is first raised to the power of 3, resulting in 8, and then that value is assigned back to x. The operator works with any numeric type that supports the ** operator, including int, float, and complex.

This operator is useful in loops or repeated calculations where the same variable needs to be raised to a power repeatedly. For instance, repeated squaring in an algorithm can be expressed concisely with **=.

How Augmented Assignment Works in Python

Augmented assignment operators are not always simply shorthand for the expanded form. For a plain variable name and an immutable value, x **= y behaves like x = x ** y: the right-hand side is evaluated first, a new object is produced, and that object is bound to the name x. With mutable targets, some augmented assignments can update the target in place; list += is a common example. The ** operation is not defined for mutable built-in types, so **= is mainly useful with built-in numeric types and user-defined classes that implement __pow__ (and optionally __ipow__).

If a class defines __ipow__, Python calls that method for **= instead of falling back to __pow__ followed by assignment. This allows mutable custom objects to update themselves in place, but for standard numeric types, the result is always a new object.

Using **= with Numeric Types

All built-in numeric types support exponentiation, so **= works with integers, floats, and complex numbers. Here are examples for each:

python
# Integer n = 5 n **= 2 print(n) # 25 # Float f = 2.5 f **= 3 print(f) # 15.625 # Complex c = 1 + 2j c **= 2 print(c) # (-3+4j)

When working with integers, note that exponentiation can produce very large numbers quickly. Python's arbitrary-precision integers handle this, but the memory and time required grow with the size of the result. For floats, the result uses the platform's floating-point semantics, so extreme values can produce inf or nan.

Evaluating the Right-Hand Side

The right-hand side of **= is evaluated once, before the assignment. This matters when the right-hand side is an expression with side effects or a function call. For example:

python
def get_power(): print("Computing power") return 2 x = 3 x **= get_power() print(x) # 9

The function is called only once, and its return value is used as the exponent. This behavior is consistent with other augmented assignment operators and matches the expanded form x = x ** get_power().

The target expression is also evaluated only once. For a subscript or attribute assignment, the target is resolved once, not twice. For example:

python
class Container: def __init__(self): self.value = 2 def get_container(): print("Getting container") return Container() get_container().value **= 3 # prints "Getting container" once

With the expanded form get_container().value = get_container().value ** 3, get_container() would be called twice. This distinction matters when the target involves a function call or a property with side effects.

Common Mistakes and Edge Cases

A frequent mistake is confusing ** with the bitwise XOR operator ^. In Python, ^ performs bitwise XOR, not exponentiation. Using x ^= 2 will not raise x to the power of 2; it will perform a bitwise operation. Always use ** for exponentiation.

Another edge case involves operator precedence. The ** operator has higher precedence than unary operators on its left, but lower precedence than unary operators on its right. For example, -2 ** 2 is evaluated as -(2 ** 2), resulting in -4. With **=, the right-hand side is evaluated as a whole, so x **= -2 correctly computes x ** (-2), which is the reciprocal of x squared.

Type errors can occur if the right-hand side is not a numeric type. For instance, x **= "2" raises a TypeError because exponentiation is not defined between an integer and a string. Similarly, using **= on a None value will fail. Always ensure the variable has a numeric value before applying this operator.

Extreme floating-point values can overflow to inf or underflow to 0.0; these are standard floating-point behaviors and are not specific to **=.

Performance and Maintainability Considerations

From a performance perspective, **= is essentially identical to x = x ** y for the numeric types where no in-place method exists. The main benefit is readability and maintainability: you avoid repeating the variable name, which reduces the chance of typos and makes the code more concise.

In tight loops where exponentiation is performed repeatedly, the cost is dominated by the exponentiation operation itself, not the assignment. For large integer exponents, the time complexity is roughly logarithmic in the exponent, but this is independent of whether you use **= or the expanded form.

One subtle performance consideration is that **= may invoke __ipow__ if the object defines it. For custom classes, implementing __ipow__ can allow in-place updates, which might be more efficient than creating a new object each time. However, for built-in numeric types, there is no in-place variant, so the behavior is identical to the expanded form.

When **= Is Not the Right Choice

There are situations where using **= is inappropriate. If you need to preserve the original value for later use, you should store it separately before applying the operator. For example:

python
original = 5 result = original **= 2 # This is invalid syntax; augmented assignments are statements, not expressions.

If you need the result as part of a larger expression, use the explicit ** operator instead:

python
original = 5 result = original ** 2

Another case is when you are working with a custom class that implements __ipow__ with side effects. If you want to avoid those side effects, use x = x ** y instead of x **= y. This forces the use of __pow__ rather than __ipow__.

Finally, if you are writing code that must be compatible with very old Python versions, note that **= has been available since Python 2.0, so it is safe to use in any modern Python codebase. There is no compatibility concern for standard Python 3.x.

In practice, **= is a clear and concise way to update a numeric variable by raising it to a power. It is especially useful in algorithms that repeatedly apply exponentiation, such as modular exponentiation or geometric progression calculations. By understanding its behavior, you can use it effectively without introducing subtle bugs.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.