How can I convert a negative number to positive in Python? (And keep a positive one.)
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Actually
abs
will return theabsolute value
of any number. Absolute value is always a non-negative number.Don't forget to check the docs.
simply multiplying by -1 works in both ways ...
The inbuilt function abs() would do the trick.
If you are working with numpy you can use
It will provide absolute values.
If "keep a positive one" means you want a positive number to stay positive, but also convert a negative number to positive, use
abs()
: