GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • See All Courses
  • See All Projects
  • See All Articles
  • Python Project Ideas
  • Best Online Learning Platforms
  • Top Coding Apps
  • Best VSCode Extensions for Developers
  • Java Project Ideas
  • Top VSCode Themes
  • Best Linux Distros for Programming
  • Top AI Coding Assistants
  • React Project Ideas
  • Best Python IDEs Compared
  • Top Python Certifications

This view

  • Original page
  • New address

GeezerVison is showing hackr.io — Python abs() Function | Docs With Examples

The Python abs() function is a built-in utility that returns the absolute value of a number in your Python programs.

It is commonly used in mathematical operations, comparisons, and real-world applications like distance calculations in Python.

Basic Syntax

The abs() function is very simple to use:

abs(number)
  • number: The numeric value whose absolute value is to be returned. This can be an integer, floating-point number, or a complex number.

Common Examples

1. Absolute Value of an Integer

print(abs(-10))

Output:

10

Explanation: The absolute value of -10 is 10, as abs() removes the negative sign.

2. Absolute Value of a Float

print(abs(-3.14))

Explanation: The absolute value of -3.14 is 3.14, ensuring it remains positive.

Output:

3.14

3. Absolute Value of a Complex Number

For complex numbers, abs() returns the magnitude:

print(abs(3 + 4j))

Explanation: The magnitude of a complex number is calculated using the formula:

|a + bj| = sqrt(a² + b²)

For 3 + 4j, we get:

sqrt(3² + 4²) = sqrt(9 + 16) = sqrt(25) = 5

Output:

5.0

Common Use Cases

1. Calculating Distance

x1, x2 = 5, -3
distance = abs(x1 - x2)
print(distance)

Explanation: The absolute difference between two points 5 and -3 gives a distance of 8.

Output:

8

2. Handling Negative Values in Finance

balance_changes = [-50, 100, -30]
positive_changes = [abs(change) for change in balance_changes]
print(positive_changes)

Explanation: This converts all negative balance changes into positive values for easier calculations.

Output:

[50, 100, 30]

3. Sorting by Absolute Value

numbers = [-10, 5, -2, -8, 3]
numbers_sorted = sorted(numbers, key=abs)
print(numbers_sorted)

Explanation: Sorting the list by absolute values results in an order that ignores negative signs.

Output:

[0, -2, 3, -5, -8, -10]

Key Takeaways

  • abs() returns the absolute value of an integer or float in your Python projects.
  • For complex numbers, abs() returns the magnitude.
  • It is useful in distance calculations, handling negative values, and sorting.

Practice Exercise

Here's a simple challenge, write a Python program that takes a list of numbers and prints their absolute values in your Python editor.

numbers = [-7, -3.5, 0, 2, 9.8]
absolute_values = [abs(num) for num in numbers]
print(absolute_values)

Wrapping Up

The abs() function is a fundamental tool in Python that simplifies working with numbers by ensuring all values are positive when needed. Understanding how it works with integers, floats, and complex numbers can help you solve mathematical and real-world problems more efficiently. Happy coding!

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.