Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python fabs

by suresh

The Python fabs function returns the absolute value ((Positive value) of the specified expression or a specific number. In this section, we discuss how to use fabs function in Python Programming language with example.

The syntax of the fabs Function in Python Programming Language to find absolute values is

Math.fabs(number);

Number: It can be a number or a valid numerical expression for which you want to find absolute value in Python.

  • If the number argument is a positive or negative number, the fabs function returns the absolute value.
  • If the number argument is not a number, the fabs function returns TypeError.

Python fabs Function Example

The fabs Function allows you to find the absolute values of numeric values. In this fabs example, we are going to find the absolute values of different data types and display the output

# Python Absolute Value Example

import math

Tup = (1, 2, 3, -4 , 5) # Tuple Declaration
Lis = [-1, 2, -3.5, -4 , 5] # List Declaration

print('Absolute value of Positive Number = %.2f' %math.fabs(10))
print('Absolute value of Negative Number = %.2f' %math.fabs(-15))

print('Absolute value of Tuple Item = %.2f' %math.fabs(Tup[3]))
print('Absolute value of List Item = %.2f' %math.fabs(Lis[2]))

print('Absolute value of Multiple Number = %.2f' %math.fabs(10 + 20 - 40))
print('Absolute value of String Number = ', math.fabs('Hello'))
Python FABS Function

First, we used the fabs Function directly on both the Positive integer and negative integer. The following Python statements find the corresponding absolute values.

print('Absolute value of Positive Number = %.2f' %math.fabs(10))
print('Absolute value of Negative Number = %.2f' %math.fabs(-15))

Next, We used the fabs Function on Tuple and List items. If you observe the above screenshot, the Python fabs function is working perfectly on them and finding absolute values.

print('Absolute value of Tuple Item = %.2f' %math.fabs(Tup[3]))
print('Absolute value of List Item = %.2f' %math.fabs(Lis[2]))

Next, We used the fabs Math function directly on multiple values.

print('Absolute value of Multiple Number = %.2f' %math.fabs(10 + 20 - 40))

It means, FABS(10 + 20 – 40)

FABS (-10) = 10

Within the last statement, we tried the fabs Function on the String value, and it returns TypeError.

print('Absolute value of String Number = ', math.fabs('Hello'))

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy