Home » Python

abs() function with example in Python

Python abs() function: Here, we are going to learn about the abs() function in Python with example.
Submitted by IncludeHelp, on March 31, 2019

Python abs() function

abs() function is a library function in Python, it is used to get the absolute value of a 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:

    abs(number)

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

Return value: It returns absolute value of number.

Example:

    Input:
    num = -10 
    print(abs(num))
    Output:
    10

    Input:
    num = 10.23
    print(float(num))
    Output:
    10.23

Python code to demonstrate example of abs() function

# 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.