๐ŸŒ
Just Academy
justacademy.co โ€บ blog-detail โ€บ absolute-difference-in-python
Absolute Difference In Python
In Python, the absolute difference between two numbers is the positive difference between them, regardless of their order. It is calculated by subtracting one number from the other and taking the absolute value of the result.
๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Recreating Absolute Difference in Python | AlgoCademy
In other words, itโ€™s taking the difference between two values (x - y) and then calculating the absolute value of the result. Create a function that takes in two numbers a and b and returns their absolute difference.
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ python difference between two numbers
Python Difference Between Two Numbers - Know Program
October 7, 2021 - (absolute difference is the difference ... number: ')) # calling function and print difference value print('The difference between numbers =', difference(num1, num2))...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ python-basic-exercise-16.php
Python: Get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference - w3resource
May 17, 2025 - These statement blocks can have any number of statements, and can include about any kind of statement. ... # Define a function named "difference" that takes an integer parameter "n" def difference(n): # Check if n is less than or equal to 17 if n <= 17: # If n is less than or equal to 17, return the absolute difference between 17 and n return 17 - n else: # If n is greater than 17, return the absolute difference between n and 17 multiplied by 2 return (n - 17) * 2 # Call the "difference" function with the argument 22 and print the result print(difference(22)) # Call the "difference" function with the argument 14 and print the result print(difference(14))
Top answer
1 of 6
6

Here is the simple one:

echo 1-$2)) | sed 's/-//'
./script.sh -10 -5

output

5

The logic is pretty simple. You provide parameters $1 and $2 to your script like ./script.sh -10 -5 and then output result of 2 using echo. sed will delete - if it is presented.

2 of 6
4

You can do simple integer arithmetic directly in the shell using the $((...)) syntax.

function abs_diff {
    local diff
    diff=1 - diff -lt 0 ]
    then
        diff=diff))
    fi
    echo $diff
}

It can be written more tersely as a single expression using the ternary ? operator.

function abs_diff {
    echo 1 >= 1 - 2 - $1))
}

Then simply use

abs_diff -10 -5

in your code.

Update: โ€œIt would be great if you could explain the logic.โ€ โ€” There we goโ€ฆ

The basic idea is that we write a Bash function that takes two integers as arguments and returns the absolute difference of them.

A Bash function can be called like an external program using the FUNCTION_NAME [ARG...] syntax. Inside a function, we can refer to its arguments via $1, $2, โ€ฆ just as we refer to a shell script's arguments outside of any function. To โ€œreturnโ€ a value from a function, we print it to standard output. (Don't abuse the return statement for that. It is intended to report success or failure, not business data.) If we want to assign the result of a function call to a variable, we can use the VAR=$(COMMAND [ARG...]) syntax. A function definition has the syntax function FUNCTION_NAME { FUNCTION_BODY }. If we declare variables that should be local to a function, we can use the local keyword. This is a Bash feature.

Now let's see how we can compute the absolute value of the difference. We only have integer arithmetic so how can we do it? Obviously, if we subtract an integer n from an integer m, there are only two possible outcomes: a non-negative or a negative result. In the first case, we are done. In the second, all we need to do is to take the negative.

The first function does exactly this.

function abs_diff {           # Define the function 'abs_diff'
    local diff                # with 'diff' as a local variable
    diff=1 - $2));        # to compute the difference of its first two arguments
    if [ $diff -lt 0 ]        # and if it is negative
    then                      # then
        diff=diff))      # negate the result
    fi                        # and
    echo $diff                # finally print the result.
}

The second version is more terse. If you know the ternary ? operator for example from C or Java, then it will come at no surprise. What this line

echo 1 >= 1 - 2 - $1))

means is: If 2 evaluates to true, then print 2, otherwise 1, which ensures that the result will always be non-negative.

If your need is a one-time thing and defining a function seems like overkill to you, simply copying the body of the second function to the place where it is needed might be a viable alternative.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-find-sum-of-absolute-difference-between-all-pairs-in-a-list
Python program to find sum of absolute difference between all pairs in a list | GeeksforGeeks
May 3, 2023 - Use nested loops to iterate through every pair of elements in the list. For each pair, compute the absolute difference between the two elements and add it to sum_abs_diff.
Find elsewhere
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python difference between two numbers | example code
Python difference between two numbers | Example code
November 2, 2023 - Simple example code. num1 = 100 num2 = 50 if num1 > num2: diff = num1 - num2 else: diff = num2 - num1 print(diff) ... The abs() method returns the absolute value of the given number.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ math โ€บ python-math-exercise-92.php
Python - Absolute difference between two consecutive digits
November 25, 2023 - Sample Data: (666) -> False (3579) -> True (2468) -> True (20420) -> False ... def test(n): return all(abs(int(x) - int(y)) == 2 for x, y in zip(str(n), str(n)[1:])) n = 666 print("Original number:", n) print("Is absolute difference between ...
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python absolute value: a step-by-step guide
Python Absolute Value: A Step-By-Step Guide | Career Karma
December 1, 2023 - The Python abs() method returns the absolute value of a number. The absolute value of a number is the numberโ€™s distance from 0. This makes any negative number positive, while positive numbers are unaffected.
๐ŸŒ
STEMpedia
ai.thestempedia.com โ€บ home โ€บ examples โ€บ positive difference of two numbers | pictoblox
Positive Difference of Two Numbers | Pictoblox - Example Project
July 31, 2023 - #Program to print the positive difference of two numbers num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if num1 > num2: diff = num1 - num2 else: diff = num2 - num1 print("The difference of",num1,"and",n...
๐ŸŒ
Tutorjoes
tutorjoes.in โ€บ Python_example_programs โ€บ check_absolute_difference_between_two_consecutive_digits_is_two_or_not
Write a Python program to check the absolute difference between two consecutive digits is two or not. Return true otherwise false
has_absolute_difference_of_two function is then called with the user's input number, and the result is stored in the ... result. If result is True, it prints "The absolute difference between consecutive digits is two," indicating that the condition is satisfied.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-15365.html
python how to find difference between two values
January 14, 2019 - Need help in giving me an idea on how to get the output I am new to python. My input is like this: Output: M Count:0 Total:50 Free: 20 A B M Count:1 Total:5
๐ŸŒ
Real Python
realpython.com โ€บ python-absolute-value
How to Find an Absolute Value in Python โ€“ Real Python
June 4, 2025 - To implement the absolute value function in Python, you can take one of the earlier mathematical definitions and translate it into code. For instance, the piecewise function may look like this: ... You use a conditional statement to check whether the given number denoted with the letter x is greater than or equal to zero. If so, then you return the same number. Otherwise, you flip the numberโ€™s sign. Because there are only two possible outcomes here, you can rewrite the above function using a conditional expression that comfortably fits on a single line:
๐ŸŒ
CodePal
codepal.ai โ€บ code generator โ€บ python function: absolute difference
Python Function: Absolute Difference - CodePal
April 27, 2023 - If either of the arguments is not an integer, the function raises a TypeError. The function then prints the absolute difference between the two numbers. To call the function, simply pass two integer arguments to the function. For more information on Python functions, check out the related links ...
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ get-the-difference-between-two-numbers-python
How to Get the Difference Between Two Numbers in Python: Complete Guide for 2026 - Difference between two numbers in python complete guide
December 18, 2025 - This guide covers the best practices, latest Python 3.14 features, and performance considerations for January 2026. The simplest way to find the difference between two numbers is using the abs() function, which returns the absolute (positive) value of any number.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-absolute-value-python-abs-tutorial
Python Absolute Value โ€“ Python abs Tutorial
June 20, 2022 - The general syntax for the abs() function looks something like this: ... The abs() function takes only one single argument, which is required. The argument is always a number which can have a negative or positive value.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ abs
Python abs()
Become a certified Python programmer. Try Programiz PRO! ... The abs() function returns the absolute value of the given number.