Videos
{1e999: x, -1e999: -x}[x*1e999]
I'm serious. This works.
The 1e999 is interpreted as a float, but it overflows the available precision, so it's inf. But infinity can be positive or negative. By multiplying x with infinity, we can coerce it into one of two values based on its sign, without using a condition operator. Then we can select x or its negation from a lookup table.
It's equivalent to the following, but doesn't require the import:
from math import inf
{inf: x, -inf: -x}[x*inf]
If you consider the built-in classes (int, str, etc) to be built-in "functions". You could do something like the following
num.__class__(('%s' % num).lstrip('-'))
Convert the number to a string, strip the negative sign and then use the original numbers class to convert back
You can do it with the built-in abs() function:
absolute_val = abs(x)
Otherwise, without the built-in function, use math:
absolute_val = (x ** 2) ** 0.5
You wanted to calculate the absolute value using python. Here Im providing the code:
Python code function: abs(Number)
Python code example: abs(-100)
Python code example: abs(105-20)
Python code example: abs(100-200)
Output:
