Ubuntu

Python abs() Function

Python abs() Function

Introduction

The abs() function in Python prints out the absolute value of a number which means it will delete the negative sign of that number.

abs() only applies to one argument such as an integer, a rational number, or complex number. This is a very useful tool in math and should be included in schools.

And we will show you how to use the abs() function in Python as you go through it below. Hope you understand.

Example

x = abs(-80)

print(x)

Output:

80

Definition

The abs() function prints out the absolute value of the specified argument.

The abs() function turns any negative number into a positive number and the positive numbers are not changed.

The absolute value of a number is always positive.

The syntax

abs(number)

Parameter Values:

number: a number (required)

More examples

Example 1: The problem of calculating the difference of 2 numbers and printing the absolute value of the result

print("Enter the 1st number:")

x = int(input())

print("Enter the 2nd number:")

y = int(input())

z = x - y

print("Result:", z)

print("Absolute:", abs(z))

Output:

Enter the 1st number: 6

Enter the 2nd number: 9

Result: -3

Absolute: 3

Example 2: With complex numbers

x = abs(3+4j)

print(x)

Output:

5.0

Conclusion

Hope you understood the tutorial on how to use the abs() function in Python.

Thanks for reading!

Similar Posts