Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Unary minus

Negates a single operand and returns the result of its numeric negation protocol.

Version: Python 3.14; repository image, production runtime not attested

Syntax

-expression

Negates a single operand and returns the result of its numeric negation protocol.

For an integer the result is its additive inverse. A class can supply __neg__ and choose the result type. The example creates a new offset, so the original instance retains its amount. An unsupported object raises TypeError.

Exponentiation binds more tightly on the left of a power: -2 ** 2 is the negation of four. Parentheses make the negative number the base, while a minus sign is valid directly in a power’s right operand.

Examples

class Offset:
    def __init__(self, amount):
        self.amount = amount

    def __neg__(self):
        return Offset(-self.amount)

delta = Offset(6)
print((-delta).amount, delta.amount)
print(-2 ** 2, (-2) ** 2, 2 ** -2)

Result

-6 6
-4 4 0.25

Exponentiation binds more tightly on the left of a power: `-2 ** 2` is the negation of four. Parentheses make the negative number the base, while a minus sign is valid directly in a power’s right operand.

Sources