What is abs in Python?

0 votes
I am new to Python. What is the use of abs in Python?
Jul 16 in Python by Neel
• 3,010 points
26 views

1 answer to this question.

0 votes

The abs() method returns the absolute value of the given number. If the number is a complex number, abs() returns its magnitude.

The abs() method takes a single argument:

  • num - number whose absolute value is to be returned. The number can be:
    1. integer
    2. floating number
    3. complex number

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

  • For integers - integer absolute value is returned
  • For floating numbers - floating absolute value is returned
  • For complex numbers - magnitude of the number is returned
Example - 
# random integer
integer = -20
print('Absolute value of -20 is:', abs(integer))

#random floating number
floating = -30.33
print('Absolute value of -30.33 is:', abs(floating))

Output- 

Absolute value of -20 is: 20
Absolute value of -30.33 is: 30.33
answered Jul 16 by Arvind
• 2,960 points

Related Questions In Python

+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
1,501 views
+4 votes
7 answers
+1 vote
3 answers
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
108 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 288 views
0 votes
1 answer

What is the difference between str() and repr() functions in Python?

str() is mostly used to create output ...READ MORE

answered Jul 8 in Python by Arvind
• 2,960 points
83 views
0 votes
1 answer

What is the difference between print and pprint in Python?

As per the documentation,  The pprint module provides a capability ...READ MORE

answered Jul 8 in Python by Arvind
• 2,960 points
165 views