Last updated: Apr 10, 2024
Reading timeยท6 min
Use the abs()
function to convert a negative number to a positive, e.g.
abs(number)
.
The abs()
function returns the absolute value of a number, which is
guaranteed to be positive.
number = -246 result = abs(number) print(result) # ๐๏ธ 246
The abs() function returns the absolute value of a number.
print(abs(-21)) # ๐๏ธ 21 print(abs(21)) # ๐๏ธ 21
The abs()
function is always going to return a positive number, regardless if
the provided argument is a positive or a negative number.
The function is also quite useful if you need to change a positive to a negative number.
number = 246 result = -abs(number) print(result) # ๐๏ธ -246
You just have to prefix the output of the abs()
function with a minus and
you're guaranteed to get a negative number.
You can also use the max()
function as an alternative to abs()
.
If you need to convert all negative numbers in a list to positive, use a list comprehension.
list_of_numbers = [-5, 14, -1, -10, 7, 1] new_list = [abs(number) for number in list_of_numbers] print(new_list) # ๐๏ธ [5, 14, 1, 10, 7, 1]
We used a list comprehension to iterate over the list.
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
On each iteration, we use the abs()
function to convert the current number to
positive.
Alternatively, you can use the max()
function
The max()
function will change the number to positive by returning the largest
value between the positive and negative counterparts of the number.
number = -246 result = max(number, -number) print(result) # ๐๏ธ 246
The max()
function returns the largest item in an iterable or the largest of
two or more arguments.
print(max(246, -246)) # ๐๏ธ 246
Regardless if the number is positive or negative, passing the number and the
number prefixed with a minus sign to max()
is guaranteed to return a positive
number.
number = 246 result = max(number, -number) print(result) # ๐๏ธ 246
If you are sure that the number is negative, you can also multiply it by -1
to
change it to a positive number.
number = -246 result = number * -1 print(result) # ๐๏ธ 246
-1
simply changes the sign of the number.If you have a positive number and multiply it by -1
, you'd get a negative
number.
number = 246 result = number * -1 print(result) # ๐๏ธ -246
You can also use the abs()
function to convert a positive number to a
negative.
The abs()
function is guaranteed to return a positive number, so by prefixing
its output with a minus, we get back a negative number.
number = 137 negative_number = -abs(number) print(negative_number) # ๐๏ธ -137
The abs() function returns the absolute value of a number.
print(abs(-13)) # ๐๏ธ 13 print(abs(13)) # ๐๏ธ 13
The abs()
function is always going to return a positive number, regardless if
the provided argument is a positive or a negative number.
All we have to do to convert a positive number to a negative is to prefix the
output of abs()
with a minus.
number = 137 negative_number = -abs(number) print(negative_number) # ๐๏ธ -137
Alternatively, you can use the min()
function.
The min()
function will change the number to negative by returning the
smallest value between the positive and negative counterparts of the number.
number = 137 negative_number = min(number, -number) print(negative_number) # ๐๏ธ -137
The min() function returns the smallest item in an iterable or the smallest of two or more arguments.
print(min(137, -137)) # ๐๏ธ -137
Regardless if the number is positive or negative, passing the number and the
number prefixed with a minus sign to min()
is guaranteed to return a negative
number.
If you are sure that the number is positive, you can also multiply it by -1
to
change it to a negative number.
number = 137 result = number * -1 print(result) # ๐๏ธ -137
-1
simply changes the sign of the number.If you have a negative number and multiply it by -1
, you'd get a positive
number.
number = -137 result = number * -1 print(result) # ๐๏ธ 137
To convert all positive numbers in a list to negative:
abs()
with a minus to change each number to
negative.my_list = [2, 4, 6, 8] new_list = [-number for number in my_list] print(new_list) # ๐๏ธ [-2, -4, -6, -8] # ------------------------------------------------ # โ If the list might contain both positive and negative numbers my_list = [-2, 4, -6, 8] new_list = [-abs(number) for number in my_list] print(new_list) # ๐๏ธ [-2, -4, -6, -8]
We used a list comprehension to iterate over the list of numbers.
On each iteration, we prefix the current number with a minus to change its sign.
my_list = [2, 4, 6, 8] new_list = [-number for number in my_list] print(new_list) # ๐๏ธ [-2, -4, -6, -8]
In this case, you have to prefix the output of the abs()
function with a
minus.
my_list = [-2, 4, -6, 8] new_list = [-abs(number) for number in my_list] print(new_list) # ๐๏ธ [-2, -4, -6, -8]
The abs function returns the absolute value of a number.
In other words, if the number is positive, the number is returned, and if the number is negative, the negation of the number is returned.
print(abs(-50)) # ๐๏ธ 50 print(abs(50)) # ๐๏ธ 50
The abs()
function always returns a positive number, so to get a negative
number, we just have to prefix the result with a minus sign.
As an alternative to using a list comprehension, you can use a for loop.
This is a three-step process:
for
loop to iterate over the list.abs()
with a minus to change each number to
negative.my_list = [-2, 4, -6, 8] new_list = [] for number in my_list: new_list.append(-abs(number)) print(new_list) # ๐๏ธ [-2, -4, -6, -8]
We used a for
loop to iterate over the list.
On each iteration, we use the abs()
function to get the positive version of
the number and prefix the result with a minus to change it to a negative number.
The list.append() method adds an item to the end of the list.
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
You can learn more about the related topics by checking out the following tutorials: