Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98843 > unrolled thread

What function is 'u0, j = random(), 0'?

Started byfl <rxjwg98@gmail.com>
First post2015-11-14 18:23 -0800
Last post2015-11-15 14:03 +1100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  What function is 'u0, j = random(), 0'? fl <rxjwg98@gmail.com> - 2015-11-14 18:23 -0800
    Re: What function is 'u0, j = random(), 0'? Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-11-14 20:47 -0600
    Learning Python from the tutorial (was: What function is 'u0, j = random(), 0'?) Ben Finney <ben+python@benfinney.id.au> - 2015-11-15 14:03 +1100

#98843 — What function is 'u0, j = random(), 0'?

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 18:23 -0800
SubjectWhat function is 'u0, j = random(), 0'?
Message-ID<f2965552-575a-44fc-9c34-dbc4792d4208@googlegroups.com>
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?

Thanks,

[toc] | [next] | [standalone]


#98849

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2015-11-14 20:47 -0600
Message-ID<mailman.346.1447556068.16136.python-list@python.org>
In reply to#98843
Hi,

On Sat, Nov 14, 2015 at 8:23 PM, fl <rxjwg98@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

[toc] | [prev] | [next] | [standalone]


#98850 — Learning Python from the tutorial (was: What function is 'u0, j = random(), 0'?)

FromBen Finney <ben+python@benfinney.id.au>
Date2015-11-15 14:03 +1100
SubjectLearning Python from the tutorial (was: What function is 'u0, j = random(), 0'?)
Message-ID<mailman.347.1447556646.16136.python-list@python.org>
In reply to#98843
Zachary Ware <zachary.ware+pylist@gmail.com> writes:

> 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.

Better than merely reading through it: Anyone who wants to learn Python
from the official tutorial should *work through* the tutorial. Run each
example, read the tutorial to get an explanation, experiment to test
whether you understand. Only then, continue on.

-- 
 \       “I bought some powdered water, but I don't know what to add.” |
  `\                                                    —Steven Wright |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web