Convert Negative number to Positive and vice versa in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
6 min

banner

# Table of Contents

  1. Convert a negative number to a Positive in Python
  2. Convert all negative numbers in a List to Positive in Python
  3. Convert a negative number to a Positive using max()
  4. Convert a positive number to a Negative in Python
  5. Convert a positive number to a Negative using min()
  6. Convert all positive numbers in a List to negative in Python

# Convert a negative number to a Positive in Python

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.

main.py
number = -246 result = abs(number) print(result) # ๐Ÿ‘‰๏ธ 246

convert negative number to positive

The code for this article is available on GitHub

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.
main.py
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.

main.py
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().

# Convert all negative numbers in a List to Positive in Python

If you need to convert all negative numbers in a list to positive, use a list comprehension.

main.py
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]

convert all negative numbers in list to positive

The code for this article is available on GitHub

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.

# Convert a negative number to a positive using max()

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.

main.py
number = -246 result = max(number, -number) print(result) # ๐Ÿ‘‰๏ธ 246

convert negative number to positive using max

The code for this article is available on GitHub

The max() function returns the largest item in an iterable or the largest of two or more arguments.

main.py
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.

main.py
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.

main.py
number = -246 result = number * -1 print(result) # ๐Ÿ‘‰๏ธ 246
However, multiplying by -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.

main.py
number = 246 result = number * -1 print(result) # ๐Ÿ‘‰๏ธ -246

# Convert a positive number to a negative in Python

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.

main.py
number = 137 negative_number = -abs(number) print(negative_number) # ๐Ÿ‘‰๏ธ -137

convert positive number to negative

The code for this article is available on GitHub

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.
main.py
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.

main.py
number = 137 negative_number = -abs(number) print(negative_number) # ๐Ÿ‘‰๏ธ -137

# Convert a positive number to a negative using min()

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.

main.py
number = 137 negative_number = min(number, -number) print(negative_number) # ๐Ÿ‘‰๏ธ -137

convert positive number to negative using min

The code for this article is available on GitHub

The min() function returns the smallest item in an iterable or the smallest of two or more arguments.

main.py
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.

main.py
number = 137 result = number * -1 print(result) # ๐Ÿ‘‰๏ธ -137
However, multiplying by -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.

main.py
number = -137 result = number * -1 print(result) # ๐Ÿ‘‰๏ธ 137

# Convert all positive numbers in a List to negative in Python

To convert all positive numbers in a list to negative:

  1. Use a list comprehension to iterate over the list.
  2. Prefix the result of calling abs() with a minus to change each number to negative.
  3. The new list will only contain negative numbers.
main.py
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]
The code for this article is available on GitHub

We used a list comprehension to iterate over the list of numbers.

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 prefix the current number with a minus to change its sign.

main.py
my_list = [2, 4, 6, 8] new_list = [-number for number in my_list] print(new_list) # ๐Ÿ‘‰๏ธ [-2, -4, -6, -8]
Note that this approach isn't sufficient if your list contains both positive and negative numbers and you only want to get a list containing negative numbers.

In this case, you have to prefix the output of the abs() function with a minus.

main.py
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.

main.py
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.

# Convert all positive numbers in a List to negative using for loop

This is a three-step process:

  1. Use a for loop to iterate over the list.
  2. Prefix the result of calling abs() with a minus to change each number to negative.
  3. Append the negative numbers to a new list.
main.py
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]
The code for this article is available on GitHub

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.

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev