Python abs() Function For Absolute Value Tutorial with Examples – POFTUT

Python abs() Function For Absolute Value Tutorial with Examples


Python provides the abs() function in order to calculate and return the absolute value of the given number. Absolute value is used in mathematics in order to make the calculation easier. The given value can be different types of a number of presentations like float, complex, negative, hexadecimal, etc.

abs() Function Syntax

abs() function has very simple syntax where only a single argument is accepted in order to calculate its absolute value.

abs(NUMBER)
  • `abs` is the function name that will return the absolute value of the given NUMBER.
  • `NUMBER`is the number we want to calculate its absolute value. NUMBER can be floating-point, negative numbers, complex numbers, hexadecimal, binary, etc.

Absolute Value For A Integer

We will start with a simple example where we will calculate the absolute value of an integer.

abs(5)
//Equal to the 5
abs(55)
//Equal to the 55
abs(550)
//Equal to the 550
abs(0)
//Equal to the 0
abs(-5)
//Equal to the 5
abs(-55)
//Equal to the 55
abs(-550)
//Equal to the 550
abs(-999)
//Equal to the 999
Absolute Value For A Integer
Absolute Value For A Integer

We can see from the examples that an integer will be converted into an integer as an absolute value. The positive integers will be converted to the same value as an absolute value. The negative integers will be converted to the positive same numbers as integers. 55 absolute value is 55 and -55 absolute value is 55too.

Absolute Value For A Floating Point

One of the most used scenarios for the abs() function or absolute value is for floating-point numbers.

abs(0.5)
//Equal to the 0.5
abs(1.5)
//Equal to the 1.5
abs(-1.5)
//Equal to the 1.5
abs(-0.5)
//Equal to the 0.5
abs(-100.9)
//Equal to the 100.9
abs(100.9)
//Equal to the 100.9
Absolute Value For A Floating Point
Absolute Value For A Floating Point

Absolute Value For A Complex Number

abs() function also can be used for complex numbers. We will provide different complex numbers in these examples.

abs(5-4j)
//Equal to the 6.4031242374328485
abs(30-4j)
//Equal to the 30.265491900843113
abs(300-4j)
//Equal to the 300.0266654815868
abs(31-4j)
//Equal to the 31.25699921617557
abs(1-4j)
//Equal to the 4.123105625617661
abs(2-4j)
//Equal to the 4.47213595499958
abs(10-40j)
//Equal to the 41.23105625617661
Absolute Value For A Complex Number
Absolute Value For A Complex Number

Absolute Value For A Binary Number

Binary numbers can be used for absolute calculation like below.

abs(0b1101101)
//Equal to the 109
abs(0b110110)
//Equal to the 54
abs(0b11011)
//Equal to the 27
abs(0b1101)
//Equal to the 13
abs(0b110)
//Equal to the 6
abs(0b11)
//Equal to the 3
abs(0b1)
//Equal to the 1
Absolute Value For A Binary Number
Absolute Value For A Binary Number

Absolute Value For Octal

We can calculate the absolute values of the octal numbers like below.

abs(0o11011010)
//Equal to the 2363912
abs(0o11011014)
//Equal to the 2363916
abs(0o1102014)
//Equal to the 295948
abs(0o1152014)
//Equal to the 316428
abs(0o152014)
//Equal to the 54284
abs(0o152614)
//Equal to the 54668
abs(0o15267)
//Equal to the 6839
Absolute Value For Octal
Absolute Value For Octal

Absolute Value For Hexadecimal

We can also use abs() function in order to calculate hexadecimal values.

abs(0x23ADF042)
//Equal to the598601794

abs(0x23ADF04)
//Equal to the37412612

abs(0x23ADF0)
//Equal to the2338288

abs(0x23ADF)
//Equal to the146143

abs(0x23AD)
//Equal to the9133

abs(0x23A)
//Equal to the570

abs(0x23)
//Equal to the35

abs(0x2)
//Equal to the2
Absolute Value For Hexadecimal
Absolute Value For Hexadecimal

Absolute Value For The List Items

Python is a practical language where we can calculate the absolute values of the given list items. We will use the map() function and provide the abs() function with the list.

numbers=[10,15,20,-10,-15-20,0.5,-0.5]

numbers_obsolute = map(abs,numbers)

print(list(numbers_obsolute))
[10, 15, 20, 10, 35, 0.5, 0.5]
Absolute Value For The List Items
Absolute Value For The List Items

Leave a Comment