Technical skill practice
Absolute Value in Python: Measure Distance from Zero with abs
Use abs(value) for absolute value in Python. For the signed real numbers in this example, it gives the distance from zero. It does not round, sort or preserve the direction of an error. Keep the original signed values when that direction remains important.
Start with a small, explicit task
Compare three fictional calibration errors against a stated tolerance of two units. Derive a magnitude for each and identify values within tolerance, including the boundary. These are synthetic arithmetic inputs, not guidance for calibrating a real instrument.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_absolute_value.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
[2.5, 0.0, 2.0]
[False, True, True]
signed source: [-2.5, 0.0, 2.0]
errors = [-2.5, 0.0, 2.0]
magnitudes = [abs(error) for error in errors]
within = [abs(error) <= 2 for error in errors]
assert magnitudes == [2.5, 0.0, 2.0]
assert within == [False, True, True]
assert errors == [-2.5, 0.0, 2.0]
assert abs(-7) == 7 and isinstance(abs(-7), int)
assert abs(-2.5) == 2.5
print(magnitudes)
print(within)
print("signed source:", errors)
Explain the result
The negative error -2.5 has magnitude 2.5, placing it outside the stated tolerance. Zero has no distance from zero, and +2 lies exactly on the included boundary. The list of booleans follows the source order, so each result can be associated with its original signed input.
The source list is unchanged. That matters because two errors with equal magnitude can point in opposite directions; applying abs and discarding the original can lose information. The integer and float checks also show that an absolute value is not a request to turn every result into an integer. The fraction in 2.5 remains present.
Avoid these mistakes
Do not use abs as a synonym for rounding a measurement. If you compare approximate floating-point results, define an appropriate comparison rule for the application instead of assuming this simple integer-like tolerance model covers every case. abs also has behavior for complex numbers, but this exercise is deliberately about signed real errors.
Questions, answered
Does abs change a negative float into an integer?
No. The checked -2.5 becomes the float 2.5, retaining its fractional magnitude.
Can I recover the sign from the absolute value?
Not by itself. Keep the signed source when direction is needed.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.