Q. Define a Python function called absolute that takes one parameter (x) and returns the absolute value of x, i.e., as shown below:
|x| = (x if x ≥ 0, -x otherwise)
Don't forget to use a return statement at the end.
Also, write the code that will demonstrate math absolute function by computing and print the absolute value of 6 and -3. Be sure to pay attention to proper indentation.
Answer :-
import math as m def absolute(x): return "Absolute value of", x, "is", m.fabs(x)
Output :-
>>> absolute(-3)
('Absolute value of', -3, 'is', 3.0)
>>> absolute(6)
('Absolute value of', 6, 'is', 6.0)
>>>
('Absolute value of', -3, 'is', 3.0)
>>> absolute(6)
('Absolute value of', 6, 'is', 6.0)
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )