[Tutor] Python tutoring

2014-01-27 Thread marcus douglas
Hello, my name is Marcus Douglas and I'm a student at College of Central 
Florida, I'm contacting you because this is my first year programming and I'm 
seriously seeking some HELP...ASAP...ThanksMarcus Douglas   
   ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Denis Heidtmann
Running python 2.7 in linux

Below are two extremes.  Can I get some guidance on this?

Thanks,
-Denis H

>>> a=zeros((2,3),dtype=int)
>>> b=a
>>> a[:,0]=[1,2]
>>> a
array([[1, 0, 0],
   [2, 0, 0]])
>>> b
array([[1, 0, 0],
   [2, 0, 0]])
>>> a=2
>>> a
2
>>> b
array([[1, 0, 0],
   [2, 0, 0]])
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python tutoring

2014-01-27 Thread Steven D'Aprano
On Sun, Jan 26, 2014 at 10:44:56PM -0500, marcus douglas wrote:

> Hello, my name is Marcus Douglas and I'm a student at College of 
> Central Florida, I'm contacting you because this is my first year 
> programming and I'm seriously seeking some HELP...ASAP...

Hello Marcus, and welcome!

We hope that we can help you, but only if you ask us questions! What are 
you having trouble with? Please don't say "everything" :-)

Remember, we're not here to do your work for you, and your College 
almost certainly has rules about plagiarism and collaboration, so please 
try to ask generic questions about the language rather than specific 
questions about homework questions. If you must ask homework questions, 
expect that we'll only give you round-about answers that hint at a 
solution.

Good questions:

"What's a variable?"

"Can you explain exceptions? I don't understand them."

"I have a for-loop, and I want to exit it early. What should I do?"

"How do I look for an item in a list? I tried "find(my_list, item) but 
it printed NameError."

"What's the best way to read a file with items separated by tabs?"

"Here is my code. I tried running it, expecting this result, but got 
this different result instead. What did I do wrong?"


Bad questions:

"Help!?"

"Here's a project I have to do for my course. Solve it for me. 
Urgently. Because I forgot to do my project for the last three weeks 
and it's due tomorrow."

"Here's some code. It doesn't work. Fix it for me."

  

Good luck, and happy programming!



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Steven D'Aprano
Hi Denis, and welcome!

On Sun, Jan 26, 2014 at 10:16:25PM -0800, Denis Heidtmann wrote:

> Running python 2.7 in linux
> 
> Below are two extremes.  Can I get some guidance on this?

In Python, = is ALWAYS an alias, never a copy, unless you explicitly do 
something to make a copy. For example, with dicts, there is a `copy` 
method:

newdict = olddict.copy()

But it's not always *obvious* that you're just aliasing the same object. 
For example, this is obvious:

py> a = []
py> b = a  # b is now a new name for a
py> a.append(42)  # modifies the list in place
py> b
[42]

but this is not:

py> a = 23
py> b = a  # b is now a new name for a
py> a = a + 1
py> b
23


In this second case, b is unchanged because `a = a + 1` doesn't *modify* 
a, it makes a new value (23 + 1 => 24) and assigns that new value to a, 
leaving b unchanged.


In your example below, I take it you are using numpy. You should say so, 
in case it is unclear.

> >>> a=zeros((2,3),dtype=int)
> >>> b=a
> >>> a[:,0]=[1,2]
> >>> a
> array([[1, 0, 0],
>[2, 0, 0]])
> >>> b
> array([[1, 0, 0],
>[2, 0, 0]])

In this example, you bind the name "a" to an array of zeroes, then bind 
the name "b" to the same array. "Name binding" is another term for 
assignment in Python. So a and b are two names for the same array. When 
you modify the array via the name "a", b sees the same changes because 
they are the same array.

Even though the line

a[:,0] = [1, 2]

looks like an assignment, it's not actually a name binding, because 
you're only assigning to a slice of the array, not the name. That makes 
it an in-place modification, just like appending to a list.

But this code that follows:

> >>> a=2
> >>> a
> 2
> >>> b
> array([[1, 0, 0],
>[2, 0, 0]])

is different. Here, the line `a = 2` is an actual name binding. It makes 
the name `a` refer to the value 2, instead of the array it used to refer 
to. But that doesn't change the array in any way, nor does it change 
`b`, so b is unaffected.


Hope this helps,


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread spir

On 01/27/2014 07:16 AM, Denis Heidtmann wrote:

Running python 2.7 in linux

Below are two extremes.  Can I get some guidance on this?

Thanks,
-Denis H


a=zeros((2,3),dtype=int)
b=a
a[:,0]=[1,2]
a

array([[1, 0, 0],
[2, 0, 0]])

b

array([[1, 0, 0],
[2, 0, 0]])

a=2
a

2

b

array([[1, 0, 0],
[2, 0, 0]])


Note: your example is strongly obscured by using weird and rare features that 
don't bring any helpful point to the actual problematic concepts you apparently 
want to deal with.


It seems you are confusing 2 issues: relation (reference) between values 
(objects) and relations between symbols (variables).


The last part of your example implies that you expect that, maybe, symbol 'b' 
may now magically point to 2 just because it were corelated with 'a', which was 
set to point to 2. Correct? If so, you are wrong: there is no relation between 
symbols in python (nore in any other language I know, for the matter). Symbols 
'a' and 'b' are independant, whatever the possible relations between their 
values. If you *replace* a's value, this has no effect on b, even if they 
previously held the very same, unique, value.


Python 3.3.2+ (default, Oct  9 2013, 14:50:09)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.

a = [1,2,3]
b = a
a is b

True

a = (1,2)   # replacement
b

[1, 2, 3]

Now, there are 2 ways to change a symbol's value: to *replace* it globally as 
above, or to *modify* it partly



a = [1,2,3]
b = a
a is b

True

a[1] = 0# modification
a

[1, 0, 3]

b

[1, 0, 3]

a is b

True

A misleading point is exemplified by "a is b": it does not mean that both 
symbols actually are the same one (unique), but that _their value objects_ are 
the same one (unique). This is the role of symbols: once defined, they are used 
for whatever they represent. Here symbols a & b just play their normal role of 
symbols, right?


The above example of modification is only possible if the value is complex (and 
mutable). Simple values like numbers or strings obviously cannot be modified, 
only replaced. Thus, such simple values just behave like "plain old values" in 
static or historical languages (in which there aren't symbols & values are 
runtime, instead plain adresses & raw data). In such languages there is 
systematic copy on assignment, unless one explicitely uses pointers. Maybe you 
are used to that, and expect such behaviour in python.


Quite the opposite, in python "symbolic assignment" (where the right side also 
is a symbol) never copies, in fact never creates a new value, but bind the left 
symbol to the same, unique value, as the right symbol.


Note: such are the semantics (the meaning) of the language. But since as said 
above this does not make any difference in practice for simple values, the 
language implementation is in fact free to copy under the hood, if this is 
simpler or more efficient to do so: the language's semantics are preserved 
nevertheless. However, python's standard implementation does not appear to do so:



a = -12.345
b = a
a is b

True# a & b are bound to the same value, there's only one -12.345

d





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Peter Otten
Denis Heidtmann wrote:

> Running python 2.7 in linux
> 
> Below are two extremes.  Can I get some guidance on this?

 a=zeros((2,3),dtype=int)
 b=a
 a[:,0]=[1,2]
 a
> array([[1, 0, 0],
>[2, 0, 0]])
 b
> array([[1, 0, 0],
>[2, 0, 0]])
 a=2
 a
> 2
 b
> array([[1, 0, 0],
>[2, 0, 0]])

In short:

x = ...

binds a name, it never copies. On the other side

x[whatever] = ...
or
x.attr = ...

can run arbitrary code that may or may not alter the object bound to x 
inplace. You have to know the type of x to know what happens.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multi Layered Graphs

2014-01-27 Thread spir

On 01/26/2014 11:23 PM, Ankit Arora wrote:

I'm working on a project which involves network graphs. Is there a library
that can help me do this:

I want to create multi-layered graphs i.e. graphs which contain a set
number of vertices but multiple 'layers' of edges i.e. same set of vertices
representing two or more properties in the same data structure.

One rather hacky solution can be to form a complete graph in igraph and
deal with the layers as if they were igraph edge attributes, though when
dealing with tens of thousands of vertices on a complete graph it will be
inefficient.

Any clue if something proper exists? If not, any more intelligent solutions
using existing libraries such as igraph/networkx?


Just a personal point of view: I usually end up implementing custom graphs or 
trees because it is rather simple [*] and because of the variety of structures 
and features. (Maybe that's why there are no general purpose node/tree/graph 
libs, only highly specialised one, as for XML parsing.)


If you take this route and need help or advice on given points or features, we 
can probably be of some use.


Denis

[*] compared the overall app: if a graph is complex, then the app it is a 
component of is even more complex

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Mark Lawrence

On 27/01/2014 09:53, spir wrote:

On 01/27/2014 07:16 AM, Denis Heidtmann wrote:

Running python 2.7 in linux

Below are two extremes.  Can I get some guidance on this?

Thanks,
-Denis H


a=zeros((2,3),dtype=int)
b=a
a[:,0]=[1,2]
a

array([[1, 0, 0],
[2, 0, 0]])

b

array([[1, 0, 0],
[2, 0, 0]])

a=2
a

2

b

array([[1, 0, 0],
[2, 0, 0]])


Note: your example is strongly obscured by using weird and rare features
that don't bring any helpful point to the actual problematic concepts
you apparently want to deal with.



Nothing weird and rare about it, just something from the numpy maths 
library and not pure Python.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Denis Heidtmann
Thanks for the responses.

The distinction between replacement and modification seems to capture the
essential aspect and helps to clarify the issue for me.

spir:
Quite the opposite, in python "symbolic assignment" (where the right side
also is a symbol) never copies, in fact never creates a new value, but bind
the left symbol to the same, unique value, as the right symbol.

If this is accurate, I can see crazy bugs in my future until I internalize
it.

Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[2,3]
>>> b=[10,20]
>>> a[0]=b[0]
>>> a
[10, 3]
>>> b[0]=100
>>> a
[10, 3]

Apparently a[0]=b[0] does not qualify as "symbolic assignment" in this
case. a[0] is not a reference to b[0].  I think I see the essential
distinction.  Experience will complete the picture for me.

-DH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multi Layered Graphs

2014-01-27 Thread Albert-Jan Roskam

--
On Sun, Jan 26, 2014 11:23 PM CET Ankit Arora wrote:

>I'm working on a project which involves network graphs. Is there a library
>that can help me do this:
>
>I want to create multi-layered graphs i.e. graphs which contain a set
>number of vertices but multiple 'layers' of edges i.e. same set of vertices
>representing two or more properties in the same data structure.
>
>One rather hacky solution can be to form a complete graph in igraph and
>deal with the layers as if they were igraph edge attributes, though when
>dealing with tens of thousands of vertices on a complete graph it will be
>inefficient.
>
>Any clue if something proper exists? If not, any more intelligent solutions
>using existing libraries such as igraph/networkx?

Maybe D3py, a Python wrapper for D3.js. No experience with it but it looks 
great.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread Danny Yoo
> Apparently a[0]=b[0] does not qualify as "symbolic assignment" in this case.
> a[0] is not a reference to b[0].  I think I see the essential distinction.
> Experience will complete the picture for me.

Yes.  The distinction is something that is blurred by Python's syntax.
 The "=" is a conceptually different thing, based on what's on the
"left hand side" of the "=".  It can means "variable binding" or
"structure mutation", and those concepts are similar, but not the same
thing.   And variable binding itself can even have a slightly
different meaning, depending on whether the surrounding context is a
function definition or not, establishing a local or global variable
binding.  Whew!

Assignment can be tricky.  It's at the heart of one of the things that
makes programming "hard": it is very much about change, about
dynamics, about having to reason what the world looks like "before"
and "after" a change.

(And hence why some customized programming languages for beginners
outright prohibit the assignment operator.)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread eryksun
On Mon, Jan 27, 2014 at 2:01 PM, Danny Yoo  wrote:
> And variable binding itself can even have a slightly
> different meaning, depending on whether the surrounding context is a
> function definition or not, establishing a local or global variable
> binding.  Whew!

Name binding is local unless you tell Python otherwise by declaring a
name to be global (also nonlocal in 3.x). CPython, for example,
compiles top-level module code to use the STORE_NAME instruction,
which stores to the locals mapping of the current frame. If the name
is declared global, it instead uses STORE_GLOBAL.

Module-level code is flagged to create a frame for which locals and
globals are the same dict. You could, however, exec code using
separate dicts for locals and globals:

src = r'''
global x
x = 0
y = 1
'''

gvars, lvars = {}, {}
exec src in gvars, lvars



>>> sorted(gvars)
['__builtins__', 'x']

>>> sorted(lvars)
['y']

CPython bytecode:

>>> code = compile(src, '', 'exec')
>>> dis.dis(code)
  3   0 LOAD_CONST   0 (0)
  3 STORE_GLOBAL 0 (x)

  4   6 LOAD_CONST   1 (1)
  9 STORE_NAME   1 (y)
 12 LOAD_CONST   2 (None)
 15 RETURN_VALUE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] When is = a copy and when is it an alias

2014-01-27 Thread eryksun
On Mon, Jan 27, 2014 at 9:07 AM, Mark Lawrence  wrote:
> On 27/01/2014 09:53, spir wrote:
>>
>> Note: your example is strongly obscured by using weird and rare features
>> that don't bring any helpful point to the actual problematic concepts
>> you apparently want to deal with.
>>
>
> Nothing weird and rare about it, just something from the numpy maths library
> and not pure Python.

NumPy arrays may seem weird to someone who expects a slice to create a
shallow copy of the data, in the way that slicing a `list` creates a
shallow copy:

>>> a = [0, 2, 4]
>>> b = a[:]

`b` is a copy of list `a`, so modifying `b` has no effect on `a`:

>>> b[:] = [1, 3, 5]
>>> a
[0, 2, 4]

Slicing a NumPy array returns a new view on the data:

a = np.array([0, 2, 4], dtype=object)
b = a[:]

>>> b.base is a
True
>>> b.flags.owndata
False

The view shares the underlying data array, so modifying it also
changes the original:

>>> b[:] = [1, 3, 5]
>>> a
array([1, 3, 5], dtype=object)

You have to ask for a `copy`:

a = np.array([0, 2, 4], dtype=object)
b = a.copy()

>>> b.base is None
True
>>> b.flags.owndata
True

>>> b[:] = [1, 3, 5]
>>> a
array([0, 2, 4], dtype=object)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor