The abs() function in Python returns the absolute value of a number, which is its distance from zero on the number line, regardless of its sign.
For integers and floats: It returns the non-negative version of the number.
Example:abs(-5)→5,abs(-3.14)→3.14For complex numbers: It returns the magnitude (distance from the origin in the complex plane), calculated as
√(real² + imag²).
Example:abs(3 + 4j)→5.0For other numeric types: It works with
Fraction,Decimal, and custom classes that define the__abs__()method.
✅
abs()is a built-in function—no import required.
❌ It does not work with non-numeric types like strings or lists—this raises aTypeError.
Common use cases include calculating distances, handling errors, and processing data where only magnitude matters (e.g., in financial analysis or machine learning).
Videos
In the formula, the numerator is the dot product of two vectors, and the denominator is the product of the norms of the two vectors.
Here is a simple way to write your formula:
import math
def dot_product(u, v):
(x1, y1) = u
(x2, y2) = v
return x1 * x2 + y1 * y2
def norm(u):
(x, y) = u
return math.sqrt(x * x + y * y)
def get_angle(u, v):
return math.acos( dot_product(u,v) / (norm(u) * norm(v)) )
def make_vector(p, q):
(x1, y1) = p
(x2, y2) = q
return (x2 - x1, y2 - y1)
#first direction vector
#punt 1, PQ
# [x,y]
P = (1,1)
Q = (5,3)
#punt 2, RS
R = (2,3)
S = (4,1)
angle = get_angle(make_vector(p,q), make_vector(r,s))
print(angle)
From what I see, the result of your code would always be pi or 0. It will be pi if one of the mu1 or mu2 is negative and when both are negative or positive it will be zero. If I remember vectors properly : Given two vectors P and Q, with say P = (x, y) and Q = (a, b) Then abs(P) = sqrt(x^2 + y^2) and P. Q = xa+yb. So that cos@ = P. Q/(abs(P) *abs(Q)). If am not clear you can give an example of what you intend to do
May be use something like this -
Copydef max_absolute_value(int1,int2):
return max(abs(int1), abs(int2))
Now with your code the None is returned because in the if block there is no return. You may have meant -
Copydef max_absolute_value(int1,int2):
value = 0
if (int1 >= int2):
value = int1
else:
value = int2
if value <= 0:
return value * -1
return value * 1
Still there is some logic issue in your code. In the if condition -5 will be less than 3. So your code will return absolute value of that i.e. is 3. Which is wrong. I think the first solution will be better for you.
You could use a one-liner including max(...), abs(...) and map(...):
Copydef max_absolute_value(int1, int2):
return max(map(abs, [int1, int2]))
result = max_absolute_value(-3, -5)
print(result)