# Python Absolute Value – Python abs Tutorial (2023)

/ #Python In this article, you will learn all about the abs() function in Python.

You will learn what the abs() function does and why you may want to use it.

You will also understand how to use abs() with the help of practical examples.

Here is what we will cover:

1. What is the abs() function in Python?
1. Why are absolute values important?
2. How to use the abs() function in Python? A syntax breakdown
2. How to use the abs() function with examples
1. How to use the abs() function with an integer argument
2. How to use the abs() function with a floating-point number argument
3. How to use the abs() function with a complex number argument

## What is The abs() Function in Python?

The abs() built-in Python function returns the absolute value of a number.

But what is an absolute value of a number in the first place?

In mathematics, the absolute value of a number refers to that number's distance from zero.

Essentially, it is how far away that number is from zero on the number line.

For example, the absolute value of the number five is five since the distance from zero to five is five units. Something to take note of is that the absolute value will always be a positive value. So, when it comes to calculating the absolute value of a negative number, the result will always be the positive version of that number.

For example, the absolute value of negative five is also five: ### Why Are Absolute Values Important?

Absolute values are an important concept and are commonly used in Math and Physics.

There may be times when you only need to use positive numbers and will not need to make use of any negative ones. In fact, you may need to make sure there are no negative numbers whatsoever for the calculations you are about to perform.

You will most likely use absolute values for calculating the distance of one point to another.

Some other common real-world examples could be:

• Calculating the difference between two points.
• Calculating the amount of energy used.
• Calculating the temperature, time, and speed differences between two points.

### How to Use the abs() Function in Python? A Syntax Breakdown for Beginners

The general syntax for the abs() function looks something like this:

abs(number)

Let's break it down:

• The abs() function takes only one single argument, which is required.
• The argument is always a number which can have a negative or positive value.
• The number can either be:
• An integer, such as 4, -15, or 10.
• A floating-point number, such as 4.1, -15.06, or 2.13.
• A complex number. A complex number is made up of two parts - a real part which consists of a real number such as 1 or 4, and an imaginary part. In Python, the imaginary part is created by adding the letter j as a suffix – not the letter i like it is in Mathematics. You add j to the end of a real number, like so: 1j or 4j.So, an example of a complex number in Python is 2 + 4j or 1 + 2j.

Now, when it comes to the return value of the abs() function:

• For integer numbers, the abs() function returns the absolute value of the given number.
• For floating point numbers, the abs() function returns the absolute value of the given number.
• For complex numbers, the abs() function returns the magnitude of the given number.

## How to Use the abs() Function with Examples

In the following sections, you will see the abs() function in action and how it behaves when it has an integer, a floating-point number, and a complex number as an argument.

### How to Use the abs() Function with an Integer Argument

When you pass an integer as an argument, the abs() function will return its absolute value.

Below is an example of passing a positive integer as an argument:

my_number = 7abs_value = abs(my_number)print(abs_value)#output #7 

And below is an example of passing a negative integer as an argument.

Remember that the absolute value will always be positive:

my_number = -17abs_value = abs(my_number)print(abs_value)#output#17

### How to Use the abs() Function with a Floating-Point Number Argument

When you pass a floating-point number as an argument, the abs() function will return its absolute value.

The following examples work in the same way as the examples from the previous section.

Here is a positive floating-point number as an argument:

my_number = 34.05abs_value = abs(my_number)print(abs_value)#output#34.05

And here is a negative floating-point number as an argument:

my_number = -43.2abs_value = abs(my_number)print(abs_value)#output#43.2

### How to Use the abs() Function with a Complex Number Argument

Complex numbers work differently from integers and floats.

When a complex number is passed as an argument to the abs() function, the return value is the magnitude of that number.

The magnitude of a complex number, such as a+bj, is the number's distance between the origin (0,0) and the point (a,b) in the complex plane. And the magnitude of a complex number is calculated with the help of the Pythagorean theorem, $\sqrt{a^2 + b^2}$

So, let's take the complex number 3 + 4j for example. You would need to calculate the square root of the squares of the numbers from the real part (3) and the imaginary part 4: $\sqrt{3^2 + 4^2}$ = 5

In Python, this is how you would use a complex number with the abs() function:

my_number = 3 + 4jabs_value = abs(my_number)print(abs_value)#output#5.0

## Conclusion

And there you have it – you now know the basics of how the abs() Python function works!

You'll start from the basics and learn in an interactive and beginner-friendly way. You'll also build five projects at the end to put into practice and help reinforce what you've learned.

Thank you so much for reading and happy coding :) Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

## FAQs

### How to do absolute value in Python? ›

Getting the absolute value of a value in Python is quick and easy to do using the abs() function. The abs() function works with integers, floats, and complex numbers, so it should keep you covered in any situation where you may need to apply it.

What is the absolute difference in Python 3? ›

Python 3 - Number abs() Method

The abs() method returns the absolute value of x i.e. the positive distance between x and zero.

How do you convert a decimal to an absolute value in Python? ›

A floating-point number in Python is a number with decimal points. We can also use the abs() function to find the absolute value of floating-point numbers. In such a scenario the abs() function returns a floating-point absolute value.

What is the absolute value of an array in Python? ›

An absolute value is a positive value of a given negative value, so it just removes the negative sign from the input value. The NumPy absolute() function takes four parameters arr , out , where , and dtype , and returns a new array with an argument value as the absolute of the source array elements.

How to do absolute value in coding? ›

In the C programming language, the use of the function abs in C is to return the absolute value of an integer. By integer, it means that the abs() is an arithmetics function. The stdlib. h library contains predefined function abs() for computing the absolute value of integers.

How do you write an absolute value function? ›

Generally, we can represent the absolute value function as, f(x) = a |x - h| + k, where a represents how far the graph stretches vertically, h represents the horizontal shift and k represents the vertical shift from the graph of f(x) = |x|.

What is the difference between abs and absolute in Python? ›

abs and np. absolute are completely identical. It doesn't matter which one you use. There are several advantages to the short names: They are shorter and they are known to Python programmers because the names are identical to the built-in Python functions.

What does abs mean in Python? ›

Python abs() Function

The abs() function returns the absolute value of the specified number.

What is relative or absolute Python? ›

An absolute file path describes how to access a given file or directory, starting from the root of the file system. A file path is also called a pathname. Relative file paths are notated by a lack of a leading forward slash. For example, example_directory.

What does += mean in Python? ›

The plus-equals operator += provides a convenient way to add a value to an existing variable and assign the new value back to the same variable. In the case where the variable and the value are strings, this operator performs string concatenation instead of addition.

### What is the rule for absolute value? ›

The absolute value (or modulus) | x | of a real number x is the non-negative value of x without regard to its sign. For example, the absolute value of 5 is 5, and the absolute value of −5 is also 5. The absolute value of a number may be thought of as its distance from zero along real number line.

What is the absolute value of 12? ›

It's easy to understand that the absolute value of 12 and -12 are identical: they're both 12, since each number is 12 units away from zero. Absolute value is always positive — to get the absolute value of a negative number, just take away the minus sign.

Which command is used to take the absolute value of a number? ›

The Math.abs() static method returns the absolute value of a number.

What is the shortcuts for absolute value? ›

To use the absolute value shortcut, simply select the cell or range of cells you want to calculate. Then, press the F4 key on your keyboard. This will automatically insert the absolute value symbol into your formula.

What is the symbol for absolute value in programming? ›

The absolute value symbol

The symbol for absolute value is a bar ∣ on each side of the number. ∣ − 6 ∣ |-6| ∣−6∣vertical bar, minus, 6, vertical bar.

How do you find the absolute value example? ›

The absolute value of the number is defined as its distance from the origin. For example, to find the absolute value of 7, locate 7 on the real line and then find its distance from the origin. To find the absolute value of −7, locate −7 on the real line and then find its distance from the origin.

Why is absolute value a function? ›

The absolute value function is commonly used to determine the distance between two numbers on the number line. Given two values a and b, then |a−b| will give the distance, a positive quantity, between these values, regardless of which value is larger.

Do you have to import abs in Python? ›

Python abs() is a built-in function that does not require any module to be imported. Whereas the fabs() is defined in the math module and requires the math module to be imported before using the function. Python's abs() function returns either an integer value or a float value based on the function input.

How do you make all numbers positive in Python? ›

When abs () is used, it converts negative numbers to positive.

How do you return a positive value in Python? ›

The abs() function is used to get the absolute (positive) value of a given number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.

### Why is there AB in front of string Python? ›

The b literal in front of the string literal means that the given string is in bytes' format. The b literal converts string into byte format. In this format bytes are actual data and string is an abstraction. A byte is a collection of 8 bits.

What are the 3 types of import statements in Python? ›

There are generally three groups:
• standard library imports (Python's built-in modules)
• related third party imports (modules that are installed and do not belong to the current application)
• local application imports (modules that belong to the current application)

What is the difference between relative and absolute file path in Python? ›

A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths.

What is absolute vs relative value? ›

Relative value is the opposite of absolute value. While absolute value examines the intrinsic value of an asset or company without comparing it to any others, relative value is based on the value of similar assets or companies.

What are the 7 operators in Python? ›

Python Operators
• Arithmetic operators.
• Assignment operators.
• Comparison operators.
• Logical operators.
• Identity operators.
• Membership operators.
• Bitwise operators.

What does == mean in Python? ›

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory.

What does += mean in coding? ›

The addition assignment ( += ) operator performs addition (which is either numeric addition or string concatenation) on the two operands and assigns the result to the left operand.

What are the 4 properties of absolute value? ›

Absolute value has the following fundamental properties: Non-negativity |a| ≥ 0. Positive-definiteness |a| = 0a = 0. Multiplicativity |ab| = |a| |b|

What is 8.5 absolute value? ›

The absolute value of 8.5 is 8.5. The absolute value of a real number a is a when a≥0, or −a when a<0. The absolute value of 8. 5 is 8.

What's the absolute value of 4? ›

The absolute value of 4 is 4 and –3 is 3. Subtract the smaller number from the larger and you get 4 – 3 = 1. The larger absolute value in the equation was 4 or a positive number so you give the result a positive result.

### What is the absolute value of 25? ›

The distance from 25 to 0 is 5 units . That means the absolute value of 25 is 5 .

What is the absolute value of 23? ›

The absolute value of a number is the distance of that number from zero. The absolute value of 23 is 23 because it is 23 units from zero.

Is absolute value always positive? ›

The absolute value of a number is never negative. Take a look at some examples. The absolute value of 5 is 5. The distance from 5 to 0 is 5 units.

How do you print negative to positive in Python? ›

In Python, positive numbers can be changed to negative numbers with the help of the in-built method provided in the Python library called abs (). When abs () is used, it converts negative numbers to positive. However, when -abs () is used, then a positive number can be changed to a negative number.

How do you take absolute value in pandas Python? ›

Pandas Series: abs() function

The abs() function is used to get a Series/DataFrame with absolute numeric value of each element. This function only applies to elements that are all numeric. Returns: Series/DataFrame containing the absolute value of each element.

How do you check if a number is positive or negative in Python? ›

Python Code:

n = float(input("Input a number: ")) if n >= 0: if n == 0: print("It is Zero! ") else: print("Number is Positive number. ") else: print("Number is Negative number. ")

What does I += 1 mean in Python? ›

i+=i means the i now adds its current value to its self so let's say i equals 10 using this += expression the value of i will now equal 20 because you just added 10 to its self. i+=1 does the same as i=i+1 there both incrementing the current value of i by 1. 3rd Jan 2020, 11:15 AM.

What is the absolute value of a negative number in Python? ›

The Python programming language defines an absolute value function, which transforms negative numbers into positive ones. For any positive number, the absolute value is equal to the original number; however, for negative numbers, the absolute value is equal to (-1) multiplied by the negative number.

How to convert negative number to positive in Python without abs? ›

# Convert a negative number to a positive using max()

The max() function will change the number to positive by returning the largest value between the positive and negative counterparts of the number. Copied! The max() function returns the largest item in an iterable or the largest of two or more arguments.

How do you reverse a negative number in Python? ›

Reverses a number:
1. Use str() to convert the number to a string, slice notation to reverse it and str. replace() to remove the sign.
2. Use float() to convert the result to a number and math. copysign() to copy the original sign.
Aug 19, 2022

### What function returns absolute value in Python? ›

The abs() function returns the absolute value of the specified number.

What is the abs value of a pandas series? ›

The abs() method in the pandas series will return a new series, which is having calculated absolute values of a series with complex numbers. The absolute value of a complex number is √a2+b2 whereas a is the real value and b is the imaginary value of a complex number.

How do you get only positive values in Python? ›

The abs() function is used to get the absolute (positive) value of a given number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned. Numeric value.

What does Elif mean in Python? ›

The elif keyword is used in conditional statements (if statements), and is short for else if.

What is Elif in Python? ›

The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".

Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated: 13/11/2023

Views: 6340

Rating: 4.1 / 5 (52 voted)

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.