-x
The prefix form of the minus sign. Same character as subtraction, different operator entirely — and it binds tighter than ** does not.
Common call
-x
Returns
the opposite sign; type is preserved for int and float
Replaces
0 - x, and x * -1
Watch out
-2 ** 2 is -4, because ** binds tighter than unary minus
-aa — Operand to negate. Any type defining __neg__ works; str and list do not.type: number · required
→ number
Demo
Live evaluation
Try:
Inputs
afloatvalue to negate
Output
-5
-5
Unary minus flips the sign and preserves the type — negating an int gives an int, negating a float gives a float. Negating an already-negative value returns to positive. Zero is the case worth knowing about beyond the demo: negating the float 0.0 gives -0.0, a genuinely distinct value that still compares equal to 0.0.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | number | yes | Operand to negate. Any type defining __neg__ works; str and list do not. |
Return value
number — The negation of a. Calls __neg__, so custom numeric types can define it.
Common patterns
Reverse a sort order
Negating the key sorts descending without a reverse flag.
items.sort(key=lambda x: -x.score)
Index from the end
The negative index that makes Python slicing so compact.
last = items[-1]
Flip a delta
Reversing a direction or an offset.
undo = -delta
Examples
1. Negates
-5
Returns
-52. Double negative
-(-5)
Returns
53. Float
-2.5
Returns
-2.54. Power binds first
-2 ** 2
Returns
-45. Parenthesised
(-2) ** 2
Returns
46. bool becomes int
-True
Returns
-1Pitfalls
1. -2 ** 2 is -4, not 4
Exponentiation binds tighter than unary minus, so the power applies to 2 and the negation happens last. This silently flips the sign in formulas ported from a calculator or another language.
Negation applied last
-2 ** 2
-4
Parenthesise
(-2) ** 2
4
2. -0.0 is a real value
Floats have a signed zero. It compares equal to 0.0, so tests pass, but it prints differently and changes the sign of the result when you divide by it.
Looks equal
-0.0 == 0.0, repr(-0.0)
(True, '-0.0')
Normalise
-0.0 + 0.0
0.0
3. Not defined for str or list
Unary minus needs __neg__, which sequences do not have. Reversing a string or list needs slicing or reversed, not a minus sign.
No __neg__
-'abc'
TypeError: bad operand type for unary -: 'str'
Slice to reverse
'abc'[::-1]
'cba'
4. bool is negated as an int
True is 1, so -True is -1 rather than False. Using a minus sign to invert a flag gives a number, and the intended logical inversion is not.
Arithmetic, not logic
-True
-1
Use not
not True
False
When to use
Use it
- Flipping a sign or a direction
- Descending sort keys without a reverse flag
- Negative indexing from the end of a sequence
Reach for something else
- Inverting a boolean → not
- Reversing a sequence → [::-1] or reversed
- Subtracting two values → the binary - operator
Notes
Complexity
O(1) for machine ints and floats; O(digits) for very large ints
Return
A new object; int and float preserve their type, bool becomes int
CPython impl
Objects/abstract.c :: PyNumber_Negative, dispatching to __neg__
Memory
Allocates a new number object
Thread-safe
Yes — numbers are immutable
FAQ
Because ** has higher precedence than unary minus, so Python computes 2 ** 2 first and negates the result. This matches standard mathematical notation, where the exponent binds to the number rather than to the sign.
-2 ** 2 # -4 (-2) ** 2 # 4
History
1.0
Unary minus present since the earliest Python.