- Syntax
- Example 1: Absolute Value of a Positive Integer
- Example 2: Absolute Value of a Negative Integer
- Example 3: Absolute Value of Zero
- Example 4: Absolute Value of a Float
- Example 5: Absolute Value in a Calculation
- Example 6: Absolute Value of a Complex Number
- Example 7: User Input
- Example 8: Comparing Two Numbers
- Example 9: Absolute Value Inside an Expression
- Example 10: Checking the Return Type
- Common Mistakes
- Best Practices
- Key Points to Remember
The absolute value of a number is its distance from zero on the number line, regardless of its direction.
Absolute values are always non-negative.
For example:
- The absolute value of
10is10. - The absolute value of
-10is also10. - The absolute value of
0is0.
In Python, the built-in abs() function is used to find the absolute value of a number.
Syntax #
abs(number)
Parameters #
| Parameter | Description |
|---|---|
number |
An integer, floating-point number, or complex number. |
Return Value #
Returns the absolute value of the given number.
Example 1: Absolute Value of a Positive Integer #
main.py
number = 25
print(abs(number))
Output #
25
Explanation #
25is already positive.- Therefore, its absolute value is
25.
Example 2: Absolute Value of a Negative Integer #
main.py
number = -25
print(abs(number))
Output #
25
Explanation #
-25is 25 units away from zero.- The
abs()function removes the negative sign. - The result is
25.
Example 3: Absolute Value of Zero #
main.py
number = 0
print(abs(number))
Output #
0
Explanation #
- Zero is neither positive nor negative.
- Its distance from zero is zero.
- Therefore, the absolute value is
0.
Example 4: Absolute Value of a Float #
main.py
temperature = -18.75
print(abs(temperature))
Output #
18.75
Explanation #
- The
abs()function also works with floating-point numbers. - The negative sign is removed.
- The decimal value remains unchanged.
Example 5: Absolute Value in a Calculation #
main.py
difference = 75 - 100
print(difference)
print(abs(difference))
Output #
-25
25
Explanation #
- The subtraction produces
-25. - The
abs()function converts it to its absolute value. - This is useful when only the magnitude of a difference is important.
Example 6: Absolute Value of a Complex Number #
main.py
number = 3 + 4j
print(abs(number))
Output #
5.0
Explanation #
- For complex numbers,
abs()returns the magnitude (also called the modulus). - For
3 + 4j, the magnitude is5.0. - The returned value is a floating-point number.
Example 7: User Input #
main.py
number = int(input("Enter an integer: "))
print(abs(number))
Sample Input #
-45
Output #
45
Explanation #
- The user enters a negative integer.
int()converts the input into an integer.abs()returns its absolute value.
Example 8: Comparing Two Numbers #
main.py
a = 12
b = 20
difference = abs(a - b)
print(difference)
Output #
8
Explanation #
a - bevaluates to-8.abs()converts it to8.- This gives the distance between the two numbers.
Example 9: Absolute Value Inside an Expression #
main.py
result = abs(-15) + abs(-20)
print(result)
Output #
35
Explanation #
abs(-15)returns15.abs(-20)returns20.- Their sum is
35.
Example 10: Checking the Return Type #
main.py
print(type(abs(-50)))
print(type(abs(-4.5)))
Output #
<class 'int'>
<class 'float'>
Explanation #
- The return type depends on the input.
- An integer input returns an integer.
- A floating-point input returns a floating-point value.
Common Mistakes #
1. Assuming abs() Always Returns an Integer #
Incorrect
print(type(abs(-4.5)))
Incorrect Expectation #
<class 'int'>
Actual Output #
<class 'float'>
Reason
abs() returns the same numeric type as its argument (except for complex numbers, where it returns a float).
2. Forgetting Parentheses #
Incorrect
number = -15
print(abs)
Output #
<built-in function abs>
Reason
abs is the function object itself.
Correct
print(abs(number))
3. Passing a String Directly #
Incorrect
print(abs("-25"))
Output #
TypeError: bad operand type for abs(): 'str'
Reason
abs() expects a numeric value.
Correct
print(abs(int("-25")))
4. Expecting the Original Variable to Change #
Incorrect
number = -10
abs(number)
print(number)
Output #
-10
Reason
abs() returns a new value.
It does not modify the original variable.
Correct
number = abs(number)
print(number)
Best Practices #
- Use
abs()whenever you need the magnitude of a number. - Store the returned value if it will be used later.
- Convert user input to a numeric type before calling
abs(). - Use
abs(a - b)when calculating the difference between two values. - Remember that
abs()works with integers, floating-point numbers, and complex numbers.
Key Points to Remember #
- The absolute value is the distance of a number from zero.
- Absolute values are always non-negative.
- Python provides the built-in
abs()function. abs()works with integers, floating-point numbers, and complex numbers.- For complex numbers,
abs()returns the magnitude. abs()returns a new value and does not modify the original variable.- The return type depends on the type of the input value.