What function is 'u0, j = random(), 0'?
Zachary Ware
zachary.ware+pylist at gmail.com
Sat Nov 14 21:47:15 EST 2015
Hi,
On Sat, Nov 14, 2015 at 8:23 PM, fl <rxjwg98 at gmail.com> wrote:
> Hi,
>
> When I read the below code, I cannot make the last line (with ######) out.
>
>
>
> def res(weights):
> n = len(weights)
> indices = []
> C = [0.] + [sum(weights[:i+1]) for i in range(n)]
> u0, j = random(), 0 ######
>
>
> If I run below code on console, it will say an error.
>
> uu, 0.1, 0
>
>
> What difference is between these two example lines?
I've noticed you sending a lot of questions in the past day or two,
many at a fairly basic level. I think you would be well-served to
read through the tutorial at https://docs.python.org/3/tutorial. It's
been a while since I last read it, but I believe this question is
covered there.
To give to an answer anyway, the two lines ("u0, j = random(), 0" and
"uu, 0.1, 0") are very different.
The first is a tuple unpacking assignment. The right side (everything
right of the equal sign) creates a tuple (comma creates a tuple, not
parentheses) of the result of calling the 'random', and 0. The left
side the equal sign is a list of names to which the right side values
are assigned; the result of random() is assigned to the name 'u0', and
0 is assigned to 'j'. The same result could be achieved by the
following two lines:
u0 = random()
j = 0
The second line attempts to make a tuple out of the value assigned to
the name 'uu', the float 0.1, and the int 0, and throw it away. I'm
assuming the error you got was a NameError because nothing was
assigned to 'uu', but I could be wrong. For future questions, you
should copy and paste the full traceback, which gives *a lot* more
information than "there was an error".
Hope this helps,
--
Zach
More information about the Python-list
mailing list