Python abs Function with Examples - Python Tutorial | Logic Practice
Python Course / Other / Python abs Function with Examples

Python abs Function with Examples

BLUF: This lesson on Python abs Function with Examples provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python abs Function with Examples

Mastering Python abs Function with Examples is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The Python function abs serves the purpose of providing the absolute value of a given number. It accepts a single parameter, which is the number for which the absolute value is to be calculated. This parameter can either be an integer or a floating-point number. In cases where the argument is a complex number, abs will return its magnitude instead.

Python abs Function Syntax

It has the following syntax:

Example

abs (num)

Parameters

num: A number whose absolute value is to be returned. The number can be:

  • integer
  • floating number
  • complex number
  • Return

It provides the absolute value of the given number.

Python abs Function Example 1

Let’s examine an illustration to determine the absolute value of a numerical figure.

Example

#  integer number

integer = -40

print('Absolute value of -40 is:', abs(integer))

#  floating number

floating = -40.83

print('Absolute value of -40.83 is:', abs(floating))

Output:

Output

Absolute value of -40 is: 40

Absolute value of -40.83 is: 40.83

Python abs Function Example 2

This example shows magnitude of a complex number.

Example

#complex number

complex = (3 - 4j)

print('Magnitude of 3 - 4j is:', abs(complex))

Output:

Output

Magnitude of 3 - 4j is: 5.0

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience