Using absolute value in Python

What is an absolute value?

According to the wikipedia article, the absolute value or modulus of a real number x, denoted |x|, is the non-negative value of x without regard to its sign. Namely |x| = x if x is a positive number and |x| = -x of x is negative, and |0| = 0.

Ok, so what does that mean? Well, basically the absolute value of any real number is the positive version of that. So, -1 -> 1, -5.3 -> 5.3.

It’s very simple and very easy to do but why would you use it?

IF you want to learn more about the mathematical side of absolute values, check out Brightstorm for more information!

Why you might need an absolute value?

Absolute value has many applications in science, math, and engineering.

Check out this Quora article for more in-depth (read: beyond my ken!) information and examples of absolute values and how they’re used.

How to use absolute value in Python

It’s pretty simple to apply the absolute value to a number in Python

x = -3
print(abs(x))

y = 0
print(abs(y))

z = 5
print(abs(z))
abs(x)
Print(x)

Output:
3
0
5

That’s all there is to it! Hope this helps.

Kids want to code? Learn Python using an intuitive and user-friendly approach with CodeMonkey. Sign up now!