TGViewer
Learn Python Coding Learn Python Coding @pythonre · 40.8K subscribers
Post #9237 1.18K
Absolute value (module) of a number

Let's say you have a negative number and you want to get its absolute value. For this, you can use the abs() function. The abs() function returns the absolute value of any number (positive, negative, and complex). Below is shown how to get a list of absolute values from a list that contains both negative and positive numbers. We use list comprehension.

list1 = [-12, -45, -67, -89, 34, 67, -13]

print([abs(num) for num in list1])


[12, 45, 67, 89, 34, 67, 13]

Also, abs() can be applied to a floating-point number, and it will return the absolute value. See below:

num = -23.12

print(abs(num))


23.12

➡️ Using the math module

If you need more advanced mathematical functions, you can use fabs() from the math module. This function always returns a float.

import math

num = -23.12
absolute_value = math.fabs(num)
absolute_value


23.12

➡️ Using a lambda function

You can also use lambda to turn a negative number into its absolute value. The code below checks if x is less than zero (that is, if it's a negative value). If so, it returns -x, essentially removing the minus and making the number positive. If x is not negative (greater than or equal to 0), it returns x as it is.

num = -23.12
absolute_value = (lambda x: -x if x < 0 else x)(num)
absolute_value


23.12
  • ❤ 5
More from @pythonre
  1. Oct 10, 2026📚 Download 10 Python books for FREE: 1. Think Python — O’Reilly https://t.co/X7y3pX68IW 2…
  2. Oct 7, 2026Python Lists: A Quick Reference 🐍 * Definition: Lists are ordered, mutable collections th…
  3. Oct 7, 2026📱 270 tools for Python developers The repository contains: 🫡 Linters and formatters; 🫡…
  4. Oct 7, 2026Hands On Python Data Science - Data Science Bootcamp Master Python for Data Science with R…
  5. Oct 6, 2026Post #9851
  6. Oct 6, 2026Python For Beginners Course In-Depth Python: A Comprehensive Introduction to Fundamental P…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →