×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python abs() Function

By IncludeHelp Last updated : December 07, 2024

Python abs() Function

The abs() function is a library function in Python, it is used to get the absolute value of a given number. It accepts a number (can be an integer number, float number, or a complex number) and returns the absolute value of the given number.

Syntax

The following is the syntax of abs() function:

abs(number)

Parameter(s):

The following are the parameter(s):

  • number – it can be an integer number, float number of a complex number.

Return Value

The abs() function returns an absolute value of the given number.

Example 1

Get the absolute values of different integer, float, and complex numbers.

# python code to demonstrate an example
# of abs() function

# integer number
iNum1 = 10
iNum2 = -10
print("Absolute value of ", iNum1, " is = ", abs(iNum1))
print("Absolute value of ", iNum2, " is = ", abs(iNum2))

# float number
fNum1 = 10.23
fNum2 = -10.23
print("Absolute value of ", fNum1, " is = ", abs(fNum1))
print("Absolute value of ", fNum2, " is = ", abs(fNum2))

# complex number
cNum1 = 10 + 5j
cNum2 = 10 - 5j
print("Absolute value of ", cNum1, " is = ", abs(cNum1))
print("Absolute value of ", cNum2, " is = ", abs(cNum2))

Output

Absolute value of  10  is =  10
Absolute value of  -10  is =  10
Absolute value of  10.23  is =  10.23
Absolute value of  -10.23  is =  10.23
Absolute value of  (10+5j)  is =  11.180339887498949
Absolute value of  (10-5j)  is =  11.180339887498949

Example 2

Get the absolute value of the difference of two numbers.

# Finding the difference of two
# numbers using abs() method

x = 10
y = 20

print("Difference between", x, "and", y, "is", abs(x - y))

Output

Difference between 10 and 20 is 10

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.