Programming languages often have built in mathematical functions, that do much more than add, subtract, multiply and divide. In this lesson,
we'll look at the absolute value function.
If you recall, the absolute value of a number is the number "made positive" by dropping the minus sign if
the number if negative and just letting the number pass if it's positive. So for example, the absolute value of -3 is 3 and the the absolute value of 3 is 3.
In math sense, this would be written as $|-3|=3$ or $|3|=3$, where the two vertical bars around a number mean absolute value. Absolute values
can also be taken of entire expressions like $|5\times 2-15|$, which would come out to be 5 (not -5).
Here is how the computer will compute an absolute value:
math.abs(some-number)
Move the mouse over a dotted box for more information.
Notice the abs in the name, which is short for "ABSolute value." The prefix of math. means that abs is included in the "math"
library that the computer knows about. The "something.something" notation is very common in computer programming.
Now you try. Use the print statement to find the absolute value of some positive and negative numbers.
Type your code here:
See your results here:
Share your code
Show a friend, family member, or teacher what you've done!