How to Convert Negative Numbers to Positive (and Vice Versa) in Python
Manipulating the sign of numbers is a fundamental task in programming. You might need to ensure a value is always positive (its absolute value) or guarantee a value is negative. Python provides straightforward ways to achieve this for both single numbers and entire lists.
This guide demonstrates how to convert negative numbers to positive and positive numbers to negative using Python's built-in abs()
function and other techniques.
Converting Negative Numbers to Positive (Absolute Value)
The goal here is to get the non-negative magnitude of a number.
Using abs()
(Recommended)
The most direct and idiomatic way to get the absolute value (which is always positive or zero) is using the built-in abs()
function.
negative_num = -15.7
zero_num = 0
positive_num = 25
print(f"Original: {negative_num}, Absolute: {abs(negative_num)}") # Output: Original: -15.7, Absolute: 15.7
print(f"Original: {zero_num}, Absolute: {abs(zero_num)}") # Output: Original: 0, Absolute: 0
print(f"Original: {positive_num}, Absolute: {abs(positive_num)}") # Output: Original: 25, Absolute: 25
abs(x)
returnsx
ifx
is positive or zero, and-x
ifx
is negative. It always returns a non-negative result. This is the standard method.
Using max()
(Alternative)
You can achieve the same result using max()
by comparing the number with its negation. The larger of the two will always be the positive (or zero) absolute value.
number = -42.5
# max(number, -number) guarantees the positive version
result = max(number, -number)
print(f"Original: {number}, Result using max(): {result}")
# Output: Original: -42.5, Result using max(): 42.5
number_pos = 30
result_pos = max(number_pos, -number_pos)
print(f"Original: {number_pos}, Result using max(): {result_pos}")
# Output: Original: 30, Result using max(): 30
While this works, abs()
is generally considered more readable and directly expresses the intent of getting the absolute value.
Conditional Multiplication (Specific Case)
If you know the input number is negative, you can multiply it by -1
to make it positive. However, this is not robust if the input could also be positive or zero, as it would incorrectly make positive numbers negative.
known_negative = -100
# Only works reliably if 'known_negative' is guaranteed to be <= 0
result = known_negative * -1
print(f"Result of multiplying {known_negative} by -1: {result}")
# Output: Result of multiplying -100 by -1: 100
potential_positive = 50
# This would incorrectly make it negative:
result_bad = potential_positive * -1
print(f"Result of multiplying {potential_positive} by -1: {result_bad}")
# Output: Result of multiplying 50 by -1: -50
# A slightly safer manual check:
num = -77
if num < 0:
num = -num # Or num *= -1
print(f"Manual check result for -77: {num}")
# Output: Manual check result for -77: 77
For general conversion, stick to abs()
.
Converting All Negative Numbers in a List to Positive
To apply the absolute value logic to every number in a list.
Using List Comprehension with abs()
This is the most concise Pythonic way.
mixed_numbers = [-5, 10, -15.5, 0, 20, -2.2]
# Create a new list where all numbers are positive (absolute values)
positive_list = [abs(number) for number in mixed_numbers]
print(f"Original list: {mixed_numbers}")
print(f"List with absolute values: {positive_list}")
# Output: List with absolute values: [5, 10, 15.5, 0, 20, 2.2]
The list comprehension iterates through the original list and applies abs()
to each number
, collecting the results in positive_list
.
Using a for
Loop with abs()
A standard for
loop achieves the same result.
mixed_numbers = [-5, 10, -15.5, 0, 20, -2.2]
positive_list_loop = [] # Initialize empty list
for number in mixed_numbers:
positive_list_loop.append(abs(number))
print(f"List with absolute values (loop): {positive_list_loop}")
# Output: List with absolute values (loop): [5, 10, 15.5, 0, 20, 2.2]
Converting Positive Numbers to Negative
The goal here is to ensure a number is negative (or zero).
Using -abs()
(Recommended)
This is the most robust way to guarantee a negative (or zero) result, regardless of the input number's original sign. abs(number)
first ensures you have the positive magnitude, and the leading -
then negates it.
positive_num = 45
negative_num = -30.3
zero_num = 0
print(f"Original: {positive_num}, Negative version: {-abs(positive_num)}")
# Output: Original: 45, Negative version: -45
print(f"Original: {negative_num}, Negative version: {-abs(negative_num)}")
# Output: Original: -30.3, Negative version: -30.3
print(f"Original: {zero_num}, Negative version: {-abs(zero_num)}")
# Output: Original: 0, Negative version: 0
This approach consistently produces the non-positive version of the number's magnitude.
Using min()
(Alternative)
You can use min()
by comparing the number with its negation. The smaller of the two will always be the negative (or zero) version.
number = 60
# min(number, -number) guarantees the negative version
result = min(number, -number)
print(f"Original: {number}, Result using min(): {result}")
# Output: Original: 60, Result using min(): -60
number_neg = -75
result_neg = min(number_neg, -number_neg) # min(-75, 75)
print(f"Original: {number_neg}, Result using min(): {result_neg}")
# Output: Original: -75, Result using min(): -75
Similar to max()
vs abs()
, this works but -abs()
is generally clearer for expressing the intent.
Conditional Multiplication (Specific Case)
If you know the input number is positive, you can multiply by -1
. Again, this is not robust for general input as it would make negative numbers positive.
known_positive = 99
# Only works reliably if 'known_positive' is guaranteed to be >= 0
result = known_positive * -1
print(f"Result of multiplying {known_positive} by -1: {result}") # Output: -99
potential_negative = -10
# This would incorrectly make it positive:
result_bad = potential_negative * -1
print(f"Result of multiplying {potential_negative} by -1: {result_bad}") # Output: 10
# A slightly safer manual check:
num = 88
if num > 0:
num = -num # Or num *= -1
print(f"Manual check result for 88: {num}") # Output: -88
Output:
Result of multiplying 99 by -1: -99
Result of multiplying -10 by -1: 10
Manual check result for 88: -88
For general conversion to a guaranteed negative (or zero), stick to -abs()
.
Converting All Numbers in a List to Negative
To ensure every number in a list is negative (or zero).
Using List Comprehension with -abs()
Apply the -abs()
logic to each element using a list comprehension.
mixed_numbers = [-5, 10, -15.5, 0, 20, -2.2]
# Create a new list where all numbers are negative (or zero)
negative_list = [-abs(number) for number in mixed_numbers]
print(f"Original list: {mixed_numbers}")
# Output: Original list: [-5, 10, -15.5, 0, 20, -2.2]
print(f"Guaranteed negative list: {negative_list}")
# Output: Guaranteed negative list: [-5, -10, -15.5, 0, -20, -2.2]
This correctly handles elements that were already negative or zero, ensuring the result reflects the negative magnitude.
Using a for
Loop with -abs()
mixed_numbers = [-5, 10, -15.5, 0, 20, -2.2]
negative_list_loop = []
for number in mixed_numbers:
negative_list_loop.append(-abs(number))
print(f"Guaranteed negative list (loop): {negative_list_loop}")
# Output: Guaranteed negative list (loop): [-5.0, -10.0, -15.5, 0.0, -20.0, -2.2]
Choosing the Right Method
- To guarantee a POSITIVE (or zero) result (Absolute Value): Use
abs()
. It's the standard, most readable, and robust method.max(n, -n)
is an alternative but less direct. Avoid manualif
or* -1
unless you have strong guarantees about the input sign. - To guarantee a NEGATIVE (or zero) result: Use
-abs()
. It's robust and clearly expresses the intent.min(n, -n)
is an alternative. Avoid manualif
or* -1
for general cases. - For Lists: Use list comprehensions (
[abs(n) ...]
or[-abs(n) ...]
) for conciseness and readability when applying the conversion to all elements.for
loops work equally well if preferred or if more complex logic per element is needed.
Conclusion
Python makes changing the sign of numbers simple and robust:
- Use the built-in
abs(number)
function to get the absolute value (guaranteed positive or zero). - Use
-abs(number)
to get the negative version of the number's magnitude (guaranteed negative or zero). These methods work reliably for both positive and negative inputs. Use list comprehensions ([abs(n) for n in list]
or[-abs(n) for n in list]
) to efficiently apply these conversions to all elements in a list. While alternatives likemax()
,min()
, or conditional multiplication exist,abs()
and-abs()
are generally the clearest and safest choices for these tasks.