+ 2
What is abs() function??
N how to understand return abs(n-50)
13 odpowiedzi
+ 6
abs is short for absolute value.
It returns the absolute value of the number passed to it.
If the input is positive, the same value is returned.
If the input is negative, the sign is removed and resulting positive value returned.
Here is official documentation:
https://docs.python.org/3/library/functions.html#abs
+ 4
The abs() function returns the absolute value of a number. Absolute value means the magnitude of the number without it's sign.
For example,
>> print(abs(-50))
50
>> print(abs(50))
50
If the number is negative, it changes the sign of the number and returns it. If the number is positive, it returns the number.
+ 2
Atul
In short it flip positive to.negative when negative value is entered and vice versa.
This was the stmt given by you right?
See, you're saying that it flips values from positive to negative and negative to positive, which means, for ex:- abs(50) results in - 50 according to your stmt, which is wrong..
abs() always returns a +ve value irrespective of the sign
+ 1
Hi! Mathematically abs() = x if x >= 0 and -x if x < 0.
So the result allways becomes >= 0.
So in your case:
If n >= 50 the result becomes n-50; otherwise it becomes -(n-50) which allways becomes >0.
Example: n = 40 =>
-(n - 50) =
- (40 - 50) =
- (-10) =
10
+ 1
sarada lakshmi My apologizes for the mistake. I will not repeat it again 😞
0
It's returns the absolute value of the given number.
Absolute value of a number is the value without considering its sign.
For ex:-
abs(-n) results in n
abs(n) results in n as well.
0
So what is n-50 here....let's suppose n is parament and then what is abs(n-50)??
0
Then the result depends on the value of 'n'
For ex:-,
Let's suppose, n = 10
abs(n-50) => abs(10-50) => abs(-40) = 40
Now,lets suppose n = 60
abs(n-50) => abs(60-50) => abs(10) =10
0
Atul Hope you got it now!!
0
sarada lakshmi thank you. Hakuna Matata
0
It is absolute function.
You can convert negative integer to positive integer.
Major problems are there on SPOJ you can see there too.
0
The abs() function in Python is a built-in function that returns the absolute value of a number. In other words, it returns the positive magnitude of a given numeric value, regardless of its sign.
For example:
abs(5) returns 5 because the absolute value of 5 is 5.
abs(-7) returns 7 because the absolute value of -7 is 7.
The abs() function is commonly used when you need to ensure that a value is positive or when you want to calculate the distance between two points.
To learn more about Python and explore the best Python tutorials, I recommend visiting https://pythondex.com.
- 1
No worries... Mistakes are common.. We have to learn from them. 👍