How to convert a negative number to positive? How to convert a negative number to positive? python python

How to convert a negative number to positive?


>>> n = -42>>> -n       # if you know n is negative42>>> abs(n)   # for any n42

Don't forget to check the docs.


simply multiplying by -1 works in both ways ...

>>> -10 * -110>>> 10 * -1-10


If "keep a positive one" means you want a positive number to stay positive, but also convert a negative number to positive, use abs():

>>> abs(-1)1>>> abs(1)1