[Tutor] learning to programming questions part 1

Alan Gauld alan.gauld at btinternet.com
Wed Jun 25 03:17:40 CEST 2014


On 25/06/14 00:23, keith papa wrote:
> 1. Hi am new to python and I have a few questions:
> Why if you want to write multiple comment you use triple quotation marks
> and not the #?

These are technically not the same as comments, they are documentation 
strings.

The Python help system will find and display them if you ask it to - it 
won't find comments (starting with #).

 >>> def f():
...    ''' dummy function with doc string'''
...    # this is not found
...    pass
...
 >>> help(f)

displays:

Help on function f in module __main__:

f()
     dummy function with doc string
(END)

So the doc string is visible in help but the comment is not.

> 2. I found this code to be interesting to me because it printed an
> output of [1,2,3,4,5,6,7] and not [1,2,3,4:4,5,6,7] why is that?
>
>>>> a= [1,2,3,4,7]
>
>>>> a[4:4] = [5,6]

[4:4] is a slice not an index. It takes a section out of an
existing list. You can see it if you play at the prompt:

 >>> a[2:4]
[3,4]

The contents between index 2(inclusive) and 4(exclusive)
Think of it as a knife sitting to the left of the indexed
items cutting out a slice...

 >>> a[4:5]
[7]

 >>> a[4:4]
[]

In your example you are replacing the empty list found by [4:4] with 
[5,6] and that gets inserted back into your original list.

>>>> print a
>
> [1,2,3,4,5,6,7]

HTH--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list