How to convert a negative number to positive?

aneuryzm picture aneuryzm · Oct 4, 2010 · Viewed 279.4k times · Source

How can I convert a negative number to positive in Python? (And keep a positive one.)

Answer

Roger Pate picture Roger Pate · Oct 4, 2010
>>> n = -42
>>> -n       # if you know n is negative
42
>>> abs(n)   # for any n
42

Don't forget to check the docs.