On Beyond Strings, 10, not -10?


#1

I'm looking at this exercise, and don't understand the output.

The Datasets are as follows:

_biggest_number(-10, -5, 5, 10)_
_smallest_number(-10, -5, 5, 10)_
_distance_from_zero(-10)_

And the values returned for each are as follows:
biggest_number = 10 = ok, this is fine
smallest_number = - 10 = again, fine
distance_from_zero = 10 = why is this 10, when it's not an option in the original dataset?

The code is as follows:

_def biggest_number(*args):_
_    print max(args)_
_    return max(args)_
_    _
_def smallest_number(*args):_
_    print min(args)_
_    return min(args)_

_def distance_from_zero(arg):_
_    print abs(arg)_
_    return abs(arg)_

Also, why is there an * in the (*args) for the first two data sets, but then only (arg) for the third - is this because there is more than one value in the first two sets?

Thanks so much, this is really bugging me!


#2

The distance_from_zero function passes its parameter to the abs built in function, which returns that absolute value of that number. The absolute value of a negative number is the negation of that numbers, which is positive.

Yes, *args then becomes a tuple of the arguments that were passed to the function.

(Edited November 23, 2016)


#3

because abs (abs - docs) returns the absolute value.

args is just a normal parameter, which means you can pass one argument on the function call

*args is special, it allows to pass as many arguments as you please, look:

def example(*args):
  print(args)
example(1,2,3)

this will give you a tulpe (an immutable list) with the arguments.


#4

Thank you very much appylpye for the explanation, I'm really grateful!


#5

Thanks ever so much stetim94, I really appreciate the explanation. :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.