Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Steven D'Aprano
On Fri, Jan 15, 2016 at 10:20:41PM -0600, boB Stepp wrote:
> At 
> https://docs.python.org/3.4/library/stdtypes.html#sequence-types-list-tuple-range
> it states:
> 
> "s.insert(i, x) inserts x into s at the index given by i (same as s[i:i] = 
> [x])"
> 
> I find this confusing.

That's because it is confusing, unless you get the missing picture. The 
missing picture is how indexes are treated in Python. For the following, 
you will need to read my email using a fixed-width (monospaced) font, 
like Courier, otherwise the ASCII diagrams won't make any sense.

First, understand that indexes are treated two ways by Python. When you 
just give a single index, like mylist[3], the indexes line up with the 
items:


mylist = [ 100, 200, 300, 400, 500 ]
indexes:^^^^^
01234

This makes perfect sense: mylist[3] is the 3rd item in the list 
(starting from zero), namely 400.

But slices are slightly different. When you provide two indexes in a 
slice, they mark the gaps BETWEEN items:

mylist = [ 100, 200, 300, 400, 500 ]
indexes:  ^^^^^   ^
  01234   5

and the slice cuts in the gaps, so for example, mylist[1:3] cuts in the 
space *just before* 200 and *just before* 400, giving you [200, 300] as 
the result.

I stress that these gaps aren't "real". It's not like Python allocates a 
list, and leaves a physical chunk of memory between each item. That 
would be just silly. You should consider these gaps to be infinitely 
thin spaces beween the consecutive items.

The important thing here is that when you slice, the list is cut 
*between* items, so slice index 1 comes immediately after the 0th item 
and immediately before the 1st item.

Now, if you're paying attention, you will realise that earlier I told a 
little fib. There's no need to say that Python treats the index 
differently for regular indexing (like mylist[3]). We just need a slight 
change in understanding:


mylist = [ 100, 200, 300, 400, 500 ]
indexes:  ^^^^^   ^
  01234   5


Now the rule becomes:

- if you have mylist[3], return the next item starting at index 3 
  (in this case, 400);

- but if you have a slice like mylist[1:3], return the items
  starting at the first index (1) and ending at the second (3), 
  namely [200, 300].

So mylist[1] is similar to mylist[1:2] except that the first returns the 
item itself, namely 200, while the second returns a one-element list 
containing that item, namely [200].


The picture is a bit more complicated once you introduce a third value 
in the slice, like mylist[1:3:2], but the important thing to remember is 
that indexes mark the gaps BETWEEN items, not the items themselves.

Now, what happens with *negative* indexes?

mylist = [ 100, 200, 300, 400, 500 ]
indexes:  ^^^^^   ^
  -6   -5   -4   -3   -2  -1

mylist[-5:-2] will be [200, 300, 400]. Easy.


Now, keeping in mind the rule that slices cut *between* items, you 
should be able to explain why mylist[1:1] is the empty list [].


What happens when you assign to a slice? The rule is the same, except 
instead of returning the slice as a new list, you *replace* that slice 
with the list given. So to understand

mylist[1:3] = [777, 888, 999]

we first mark the gaps between items and underline the slice being 
replaced:

mylist = [ 100, 200, 300, 400, 500 ]
indexes:  ^^^^^   ^
  01234   5
   ---

and insert the new slice in its place (moving everything to the right 
over):

mylist = [ 100, 777, 888, 999, 400, 500 ]
indexes:  ^^^^^^   ^
  012345   6


Now let's look at an insertion. We're told that 

mylist.insert(1, x)

is the same as mylist[1:1] = [x]. Let's see:


mylist = [ 100, 200, 300, 400, 500 ]
indexes:  ^^^^^   ^
  01234   5
   -

Note the tiny underline that extends from index 1 to index 1. Since that 
doesn't extend to the next index, no items are removed. Now insert the 
new list [x] into that slice:

mylist = [ 100, x, 200, 300, 400, 500 ]
indexes:  ^^  ^^^^   ^
  01  2345   6
   
And that's an insertion! The insertion takes place at position one, 
which means that x ends up just after the gap at position one.



> The second thing I find puzzling is the docs say x is inserted at
> position i, while in the interpreter:
> 
> >>> help(list.insert)
> Help on method_descriptor:
> 
> insert(...)
> L.insert(index, object) -- insert object before index
> 
> The "...insert object before index" makes sense to me, but "...inserts
> x into s at the index given by i..." does not because:

The beauty of thinking about indexs as the gap between items is that it 
doesn't matter whether you insert "after" the gap or "before" the gap or 
"into" the gap, you get the same thing:

mylist = [ 100, x, 200, 300, 

Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Peter Otten
Steven D'Aprano wrote:

> But slices are slightly different. When you provide two indexes in a 
> slice, they mark the gaps BETWEEN items:

The other explanation that Python uses half-open intervals works for me. 

> Now, what happens with *negative* indexes?
> 
> mylist = [ 100, 200, 300, 400, 500 ]
> indexes:  ^^^^^   ^
>   -6   -5   -4   -3   -2  -1
> 
> mylist[-5:-2] will be [200, 300, 400]. Easy.

>>> mylist = [ 100, 200, 300, 400, 500 ]
>>> mylist[-5:-2]
[100, 200, 300]

Off by one, you picked the wrong gaps.

Slightly related is a problem that comes up in practice; you cannot specify 
"including the last item" with negative indices:

>>> for i in reversed(range(len(mylist))):
... print(mylist[:-i])
... 
[100]
[100, 200]
[100, 200, 300]
[100, 200, 300, 400]
[]

A simple fix is

>>> for i in reversed(range(len(mylist))):
... print(mylist[:-i or None])
... 
[100]
[100, 200]
[100, 200, 300]
[100, 200, 300, 400]
[100, 200, 300, 400, 500]

The hard part is to remember to test whenever a negative index is 
calculated.

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


[Tutor] What is the square brackets about?

2016-01-16 Thread Ege Berkay Gülcan
def get_(loc, thing):
if loc==[]: return thing
return get_(loc[1:], thing[loc[0]])

Hi I am new to Python and I would like to learn about these uses of square
brackets. I know that loc[1:] means loc list without the first element but
I do not know the meanings of loc==[] and thing[loc[0]].

Bu
e-posta Avast tarafından korunan virüssüz bir bilgisayardan gönderilmiştir.
www.avast.com

<#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread Alan Gauld
On 16/01/16 15:51, Ege Berkay Gülcan wrote:
> def get_(loc, thing):
> if loc==[]: return thing
> return get_(loc[1:], thing[loc[0]])
> 
> Hi I am new to Python and I would like to learn about these uses of square
> brackets. I know that loc[1:] means loc list without the first element but
> I do not know the meanings of loc==[] and thing[loc[0]].

[] means an empty list.
so

if loc==[]: return thing

tests whether loc is empty.

Python treats an empty list as being 'false' so the test
could just as easily have been written:

if loc:

thing[loc[0]]

is simply applying an index to thing.

loc[0] returns the value of the first item in loc.
lets assume that is 2

then
thing[loc[0]]
would be the same as
thing[2]

As a side note, this function looks very fragile since
it depends on thing having nested data structures that
match the indexes provided by loc.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread Chris Warrick
On 16 January 2016 at 16:51, Ege Berkay Gülcan  wrote:
> def get_(loc, thing):
> if loc==[]: return thing
> return get_(loc[1:], thing[loc[0]])
>
> Hi I am new to Python and I would like to learn about these uses of square
> brackets. I know that loc[1:] means loc list without the first element but
> I do not know the meanings of loc==[] and thing[loc[0]].

loc == [] checks “if `loc` is equal to an empty list”. Note that this
is not a good way to do this. A much better way to spell this would
be:

if not loc:
return thing

thing[loc[0]] means “check what the 0th element of `loc` (`loc[0]`)
is, and use it as an index for `thing` (`thing[…]`).

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


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread boB Stepp
On Sat, Jan 16, 2016 at 1:00 PM, Alan Gauld  wrote:

> As a side note, this function looks very fragile since
> it depends on thing having nested data structures that
> match the indexes provided by loc.

As Alan's response arrived, I was in the interpreter trying out this
function with a set of values which would enable it to work.  Perhaps
it will illustrate one thing it could do.  It is hard for me to
determine the original intent of the function as the names do not
provide much context.  Perhaps "loc" is short for "location" within
the "thing"?

>>> def get_(loc, thing):
if loc == []: return thing
return get_(loc[1:], thing[loc[0]])

>>> loc = [2]
>>> thing = ['cat', 'dog', 'mouse', 'tick']
>>> get_(loc, thing)
'mouse'

Notice that "thing" appears to work for any "thing" that can be
indexed.  For instance, if I now make "thing" a tuple, the function
will still behave:

>>> thing = ('cat', 'dog', 'mouse', 'tick')
>>> get_(loc, thing)
'mouse'

While learning I find it very helpful to either use IDLE or invoke the
Python interpreter in the shell and try these things out.  Once I get
it to work, then I play around with the syntax and deliberately try to
break things and see what sorts of errors are generated, figure out
the limits of what the syntax will allow, etc., until I feel I am
starting to understand what the original code does.

HTH,

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


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread boB Stepp
On Sat, Jan 16, 2016 at 1:14 PM, boB Stepp  wrote:

>
> While learning I find it very helpful to either use IDLE or invoke the
> Python interpreter in the shell and try these things out.  Once I get
> it to work, then I play around with the syntax and deliberately try to
> break things and see what sorts of errors are generated, figure out
> the limits of what the syntax will allow, etc., until I feel I am
> starting to understand what the original code does.

Continuing to play around with the code:

>>> loc = [0, 2, 8]
>>> get_(loc, thing)
Traceback (most recent call last):
  File "", line 1, in 
get_(loc, thing)
  File "", line 3, in get_
return get_(loc[1:], thing[loc[0]])
  File "", line 3, in get_
return get_(loc[1:], thing[loc[0]])
  File "", line 3, in get_
return get_(loc[1:], thing[loc[0]])
IndexError: string index out of range

and,

>>> loc = [0, 1]
>>> get_(loc, thing)
'a'

And so on.  Until you (and I) can understand why the function produces
these outputs with the given values of loc and thing, then we cannot
claim we understand what is going on.  So I encourage you to
thoroughly explore your sample code!



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


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread Alan Gauld
On 16/01/16 19:35, boB Stepp wrote:

> And so on.  Until you (and I) can understand why the function produces
> these outputs with the given values of loc and thing, then we cannot
> claim we understand what is going on.  So I encourage you to
> thoroughly explore your sample code!

The function plumbs the depths of thing according to the indices
supplies in loc. The shape of thing must match the length of loc.

So if loc = [1,2,3]

thing must be a sequence of sequences of sequences
where the final sequence os at least 4 items long.

That's why I said it was a very fragile function.
Any mismatch in the data is likely to give an
IndexError.

I'd strongly recommend wrapping the second line
in a try/except - at least to aid debugging.

def get_(loc, thing):
if loc==[]: return thing
try: return get_(loc[1:], thing[loc[0]])
except IndexError:
 print "cannot access index", loc[0]," of", thing
 raise

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread boB Stepp
On Sat, Jan 16, 2016 at 6:19 AM, Peter Otten <__pete...@web.de> wrote:
> Steven D'Aprano wrote:
>
>> But slices are slightly different. When you provide two indexes in a
>> slice, they mark the gaps BETWEEN items:
>
> The other explanation that Python uses half-open intervals works for me.
>
>> Now, what happens with *negative* indexes?
>>
>> mylist = [ 100, 200, 300, 400, 500 ]
>> indexes:  ^^^^^   ^
>>   -6   -5   -4   -3   -2  -1

So in this model of understanding negative list indexing, should it be:

mylist = [ 100, 200, 300, 400, 500 ]
  ^^^^^   ^
  -5   -4   -3   -2   -1  ?

Well, it has to be this; otherwise, the off-by-one error exist.  This
also continues to explain why

mylist.insert(-1, x)

inserts x *before* 500.  But in this model, what should go in the place of "?"?


> Slightly related is a problem that comes up in practice; you cannot specify
> "including the last item" with negative indices:

[...]

> A simple fix is
>
 for i in reversed(range(len(mylist))):
> ... print(mylist[:-i or None])
> ...
> [100]
> [100, 200]
> [100, 200, 300]
> [100, 200, 300, 400]
> [100, 200, 300, 400, 500]

OK, Peter, all was going smoothly in boB-land until you added your
"fix".  Adding "or None" has me addled!  I tried to clarify things in
the interpreter (I removed "reversed" so that I could deal only with
what I was finding confusing.):

>>> for i in range(len(mylist)):
print(mylist[:-i])

[]
[100, 200, 300, 400]
[100, 200, 300]
[100, 200]
[100]

Then adding the "or None":

>>> for i in range(len(mylist)):
print(mylist[:-i or None])

[100, 200, 300, 400, 500]
[100, 200, 300, 400]
[100, 200, 300]
[100, 200]
[100]

So far I've duplicated what you did without the reversed built-in.  So
I tried playing around:

>>> mylist[:0]
[]

This was expected as this is equivalent to mylist[0:0].

>>> mylist[:0 or None]
[100, 200, 300, 400, 500]

The critical portion of the for loop for me to understand, since it
results in [100, 200, 300, 400, 500] instead of the empty list.  But
what the heck is going on here?

>>> mylist[0 or None]
Traceback (most recent call last):
  File "", line 1, in 
mylist[0 or None]
TypeError: list indices must be integers, not NoneType

And I am stuck.  I can't figure out why [:0 or None] is legal and what
it is actually doing, while [0 or None] is (a rather obvious)
TypeError.  Please illuminate my darkness!

> The hard part is to remember to test whenever a negative index is
> calculated.

I am assuming that this is relevant to what just came before, the use
of this "or None" check.  Is this correct?


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


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread boB Stepp
On Sat, Jan 16, 2016 at 2:33 PM, Alan Gauld  wrote:
> On 16/01/16 19:35, boB Stepp wrote:
>
>> And so on.  Until you (and I) can understand why the function produces
>> these outputs with the given values of loc and thing, then we cannot
>> claim we understand what is going on.  So I encourage you to
>> thoroughly explore your sample code!
>
> The function plumbs the depths of thing according to the indices
> supplies in loc. The shape of thing must match the length of loc.

The interesting part of this function for me was not the OP's original
questions, but why this particular use of a recursive function (Am I
using the proper terminology here?)?  Can someone provide a practical
use of this type of function where some more straightforward
searching/parsing approach would be ineffective?

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


[Tutor] Console application

2016-01-16 Thread Ali Moradi
Hi, i don't have any clue how to write a console program that shows a list
of options which could be chosen with keyboard and when one item was
selected, a text shows there.

I want to know, which things i need to write a program like that?

Input() , print, and  What? :(
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Source of MySQL Command Interpreter

2016-01-16 Thread Ricardo Martínez
Hi, i wrote a small APP to execute MySQL commands and retrieve to a Treeview

http://pastebin.com/v2C8kAu1

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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Alan Gauld
On 16/01/16 22:39, boB Stepp wrote:

> So in this model of understanding negative list indexing, should it be:
> 
> mylist = [ 100, 200, 300, 400, 500 ]
>   ^^^^^   ^
>   -5   -4   -3   -2   -1  ?
> 
> Well, it has to be this; otherwise, the off-by-one error exist.  This
> also continues to explain why
> 
> mylist.insert(-1, x)
> 
> inserts x *before* 500.  But in this model, what should go in the place of 
> "?"?

-0

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Ben Finney
boB Stepp  writes:

> So in this model of understanding negative list indexing, should it be:
>
> mylist = [ 100, 200, 300, 400, 500 ]
>   ^^^^^   ^
>   -5   -4   -3   -2   -1  ?

For completeness, let's use the rest of the integers also::

  012345
  ↓↓↓↓↓↓
mylist = [ 100, 200, 300, 400, 500  ]
  ↑↑↑↑↑↑
 −5   −4   −3   −2   −1?

> But in this model, what should go in the place of "?"?

You can use ‘len(mylist)’ for the index at the end of the sequence.

There isn't a negative number which will address that position; it isn't
needed, because there is already one obvious way :-)

-- 
 \“Without cultural sanction, most or all of our religious |
  `\  beliefs and rituals would fall into the domain of mental |
_o__) disturbance.” —John F. Schumaker |
Ben Finney

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


Re: [Tutor] Source of MySQL Command Interpreter

2016-01-16 Thread Ben Finney
Ricardo Martínez  writes:

> Hi, i wrote a small APP to execute MySQL commands and retrieve to a Treeview
>
> Share your comments and upgrades.

You're addressing this to the wrong forum. If you want to discuss code,
please post *small, complete* samples of code directly here in the forum
and ask specific questions.

Large code bases are not appropriate here; we are better geared to
discussing specific problems and concepts.

Links to code elsewhere are not appropriate here; this forum is geared
to discussing the code in context with the messages.

So please keep code examples small, and put them in your message for
discussion.

-- 
 \ “I call him Governor Bush because that's the only political |
  `\  office he's ever held legally.” —George Carlin, 2008 |
_o__)  |
Ben Finney

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


Re: [Tutor] What is the square brackets about?

2016-01-16 Thread Alan Gauld
On 16/01/16 22:53, boB Stepp wrote:

>> The function plumbs the depths of thing according to the indices
>> supplies in loc. The shape of thing must match the length of loc.
> 
> The interesting part of this function for me was not the OP's original
> questions, but why this particular use of a recursive function (Am I
> using the proper terminology here?)?  

Yes it's a recursive function.


> Can someone provide a practical
> use of this type of function where some more straightforward
> searching/parsing approach would be ineffective?

In fact its much easier to write this using recursion that
any other method. Its just very difficult to use and so
needs some protection. The difficulty is in getting the
input data structures to match, once that's done the
function itself is very straightforward.

It's a way of quickly navigating a tree structure where the
route to the node (or subtree) of interest is given in
advance. Think of 'loc' as being a directory path broken
into its individual parts and 'thing' as being the file
system represented as a dictionary of dictionaries (for
the named indexing to work). [BTW I haven't tried that to
see if it works it's just a guess at a possible
application...] Of course, for a real filesystem we have
functions in the os module to do the work but that type
of data structure might be the target.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Console application

2016-01-16 Thread Ben Finney
Ali Moradi  writes:

> Hi, i don't have any clue how to write a console program that shows a
> list of options which could be chosen with keyboard and when one item
> was selected, a text shows there.

A text shows where? Shows at the point of input, at the point of the
option, at some other point?

> I want to know, which things i need to write a program like that?

I think you might want to address specific points in the grid of
characters on the terminal. That's specific to each operating system, so
you'll need an API which knows how to tell the terminal what you mean.

The Python standard library includes such an API in the ‘curses’ library
https://docs.python.org/3/library/curses.html>. Try using that and
see whether it meets your needs.

-- 
 \“Telling pious lies to trusting children is a form of abuse, |
  `\plain and simple.” —Daniel Dennett, 2010-01-12 |
_o__)  |
Ben Finney

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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Alan Gauld
On 16/01/16 23:56, Alan Gauld wrote:
> On 16/01/16 22:39, boB Stepp wrote:
> 
>> So in this model of understanding negative list indexing, should it be:
>>
>> mylist = [ 100, 200, 300, 400, 500 ]
>>   ^^^^^   ^
>>   -5   -4   -3   -2   -1  ?
>>
>> Well, it has to be this; otherwise, the off-by-one error exist.  This
>> also continues to explain why
>>
>> mylist.insert(-1, x)
>>
>> inserts x *before* 500.  But in this model, what should go in the place of 
>> "?"?
> 
> -0
> 

I should have added a :-/ to that in case it wasn't obvious...

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Console application

2016-01-16 Thread Alan Gauld
On 16/01/16 21:18, Ali Moradi wrote:
> Hi, i don't have any clue how to write a console program that shows a list
> of options which could be chosen with keyboard and when one item was
> selected, a text shows there.
> 
> I want to know, which things i need to write a program like that?
> 
> Input() , print, and  What? :(


There are lots of ways to do this. The simplest is simply to
display a menu and ask the user to select an entry.

print('''
1 New
2 Edit
3 Delete
4 quit
''')
cmd = int(input('Enter an option number')

if cmd == 1:
elif cmd == 2:
etc

If the above explanation is still too complicated
then please come back with more questions

You can add loops and error traps if you wish.
You can put the menu inside a function. But I don't
know how much of that extra stuff you understand yet.

Another, more advanced/professional, option, which builds
applications that look like Python's help() function, is
to use the cmd module. There are examples in the documentation

Finally, if you are on *nix or MacOS you can use curses
to build a pseudo-GUI that works in a terminal. But
that is almost certainly too much for you at this stage.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Source of MySQL Command Interpreter

2016-01-16 Thread Alan Gauld
On 17/01/16 00:04, Ben Finney wrote:
> Ricardo Martínez  writes:
> 
>> Hi, i wrote a small APP to execute MySQL commands and retrieve to a Treeview
>>
>> Share your comments and upgrades.
> 
> You're addressing this to the wrong forum. If you want to discuss code,
> please post *small, complete* samples of code directly here in the forum
> and ask specific questions.

To be fair to Ricardo, he did ask permission(15th Jan)
and I said it would be fine and, if the code was more
that 200 lines he could put it on a pastebin. It's 178
lines so he is well within limits.

And there are quite a few precedents for folks asking
for a critique of their code on the tutor list. It's
all part of the learning process - provided its not
too big.

Whether people take the time to read/comment is, of
course, up to them.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Alex Kleider

On 2016-01-16 16:08, Alan Gauld wrote:

On 16/01/16 23:56, Alan Gauld wrote:

On 16/01/16 22:39, boB Stepp wrote:

So in this model of understanding negative list indexing, should it 
be:


mylist = [ 100, 200, 300, 400, 500 ]
  ^^^^^   ^
  -5   -4   -3   -2   -1  ?

Well, it has to be this; otherwise, the off-by-one error exist.  This
also continues to explain why

mylist.insert(-1, x)

inserts x *before* 500.  But in this model, what should go in the 
place of "?"?


-0



alex@x301:~$ python3
Python 3.4.3 (default, Oct 14 2015, 20:33:09)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.

mylist = [1, 2, 3, 4, 5]
mylist[0:None]

[1, 2, 3, 4, 5]

mylist[0:-0]

[]

-0

0




It appears that None provides a surrogate for -0 which itself evaluates 
to 0.





I should have added a :-/ to that in case it wasn't obvious...


It wasn't to me; could you please explain what you mean by ":-/" and/or 
where you should have added it?



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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread boB Stepp
Alex sent me this off-list.  I hope he does not mind me sharing part
of what he wrote on-list!

On Sat, Jan 16, 2016 at 4:57 PM, Alex Kleider  wrote:
> On 2016-01-16 14:39, boB Stepp wrote:
>
>
> mylist[:0 or None]
>>
>> [100, 200, 300, 400, 500]
>>
>> The critical portion of the for loop for me to understand, since it
>> results in [100, 200, 300, 400, 500] instead of the empty list.  But
>> what the heck is going on here?

[...]

> I guess when used in slices, None represents '-0'; clever really.
> A way to distinguish -0 from 0 which in simple arithmetic are one and the
> same.

I have no clue whether Alex's hypothesis is what actually goes on in
the implementation details, but things sure act like he is correct.
This led me to try:

>>> mylist[:None]
[100, 200, 300, 400, 500]

So, in effect, None is acting as a place holder for that final
position in slices.  Also, I would never have thought to be able to
use a logical "or" inside an index in Peter's "[:-i or None]".


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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Cameron Simpson

On 16Jan2016 18:43, boB Stepp  wrote:

This led me to try:


mylist[:None]

[100, 200, 300, 400, 500]

So, in effect, None is acting as a place holder for that final
position in slices.  Also, I would never have thought to be able to
use a logical "or" inside an index in Peter's "[:-i or None]".


Yah, like the default value for many missing parameters. When you don't need an 
expression after the ":" you can of course write:


 mylist[:]

much like writing a function "def f(x, y=None)"; None is a sentinel value - 
specially recognised as nor in the normal domain for that value.


Cheers,
Cameron Simpson 

Q: How does a hacker fix a function which doesn't work for all of the elements 
in its domain?

A: He changes the domain.
- Rich Wareham 

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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Alex Kleider

On 2016-01-16 18:02, Cameron Simpson wrote:

On 16Jan2016 18:43, boB Stepp  wrote:

This led me to try:


mylist[:None]

[100, 200, 300, 400, 500]

So, in effect, None is acting as a place holder for that final
position in slices.  Also, I would never have thought to be able to
use a logical "or" inside an index in Peter's "[:-i or None]".


Yah, like the default value for many missing parameters. When you
don't need an expression after the ":" you can of course write:

 mylist[:]

much like writing a function "def f(x, y=None)"; None is a sentinel
value - specially recognised as nor in the normal domain for that
value.



Can you please clarify the last bit:
"specially recognised as nor in the normal domain for that value."

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


Re: [Tutor] s.insert(i, x) explanation in docs for Python 3.4 confusing to me

2016-01-16 Thread Steven D'Aprano
On Sat, Jan 16, 2016 at 01:19:16PM +0100, Peter Otten wrote:
> Steven D'Aprano wrote:
> 
> > But slices are slightly different. When you provide two indexes in a 
> > slice, they mark the gaps BETWEEN items:
> 
> The other explanation that Python uses half-open intervals works for me. 

Half-open at the start or the end? :-)

You're right, "half-open intervals" (open at the end) is also a good way 
of thinking about it. It certainly helps with things like range(), which 
don't involve slicing/cutting. You just memorize the the start value is 
included and the end value is not.

If you think of indexes falling between elements, and slicing along 
those gaps, then the natural consequence is a half-open interval. Take 
mylist[1:6] for example:


  0 1 2 3 4 5 6 7 8
mylist = [ a b c d e f g h ]
| |
cut hereand here


By cutting *between* a and b, and f and g, you naturally get a half open 
interval: mylist[1] is included, but mylist[6] is not.

But if you think of indexes being aligned with the items themselves, you 
need to memorize a special rule "do I include the item or not?":


   0 1 2 3 4 5 6 7
mylist = [ a b c d e f g h ]
 | |
  include   exclude
   this  this



> > Now, what happens with *negative* indexes?
> > 
> > mylist = [ 100, 200, 300, 400, 500 ]
> > indexes:  ^^^^^   ^
> >   -6   -5   -4   -3   -2  -1
> > 
> > mylist[-5:-2] will be [200, 300, 400]. Easy.
> 
> >>> mylist = [ 100, 200, 300, 400, 500 ]
> >>> mylist[-5:-2]
> [100, 200, 300]
> 
> Off by one, you picked the wrong gaps.

Oops, so I did. You're absolutely right.

mylist = [ 100, 200, 300, 400, 500 ]
indexes:  ^^^^^   ^
  -5   -4   -3   -2  -1   

The last index ought to be "-0" in some sense, but of course that's just 
zero which is the first index. So as you say:


> Slightly related is a problem that comes up in practice; you cannot specify 
> "including the last item" with negative indices:

But you can do so by leaving the end index blank:

py> mylist = [ 100, 200, 300, 400, 500 ]
py> mylist[-2:-1]
[400]
py> mylist[-2:0]
[]
py> mylist[-2:]
[400, 500]



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