Что делает abs в питоне

Python Absolute Value – Python abs Tutorial

Dionysia Lemonaki

Dionysia Lemonaki

Python Absolute Value – Python abs Tutorial

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:

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.

Screenshot-2022-06-14-at-9.51.34-AM

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:

Screenshot-2022-06-14-at-10.01.18-AM

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:

Оцените статью