BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python abs() Function with examples

By Chaitanya Singh | Filed Under: Python Tutorial

Python abs() Function returns the absolute (non-negative value) value of a number. For example, absolute value of -5 is 5 and absolute of 5 is also 5. In this guide, we will see how to use abs() function in Python with the help of examples.

Python abs() function works on?

The abs() function works on following numbers:
1. Integers, for example 6, -6, 1 etc.
2. Floating point numbers, for example 5.34, -1.44 etc
3. Complex numbers, for example 3+4j, 4+6j etc.

Python abs() example

# integer number
num = -5
print('Absolute value of -5 is:', abs(num))

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

Output:
Python abs() function example

Python abs() function for complex numbers example

When a complex number is passed as an argument to abs() function, it returns the magnitude of the complex number. The magnitude of a complex number a + bj is equal to √a2+b2.

# complex number
cnum = 4 - 5j
print('Absolute value of 4 - 5j is:', abs(cnum))

cnum2 = 3 + 4j
print('Absolute value of 3 + 4j is:', abs(cnum2))

Output:
Python abs function complex numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap