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


Groups > comp.lang.python > #98849

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

From Zachary Ware <zachary.ware+pylist@gmail.com>
Newsgroups comp.lang.python
Subject Re: What function is 'u0, j = random(), 0'?
Date 2015-11-14 20:47 -0600
Message-ID <mailman.346.1447556068.16136.python-list@python.org> (permalink)
References <f2965552-575a-44fc-9c34-dbc4792d4208@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web