On Sat, Dec 17, 2016 at 1:10 PM, John <miao...@gmail.com> wrote:
>
> Hi,
>
>    I am new to Python, and I believe it's an easy question. I know R and 
> Matlab.
>
> ************
> >>> x=[1,2,3,4,5,6,7]
> >>> x[0]
> 1
> >>> x[1:5]
> [2, 3, 4, 5]
> *************
>
>     My question is: what does x[1:5] mean? By Python's convention, the first 
> element of a list is indexed as "0". Doesn't x[1:5] mean a sub-list of x, 
> indexed 1,2,3,4,5? If I am right, it should print [2,3,4,5,6]. Why does it 
> print only [2,3,4,5]?
>

What you are asking about is "slicing".  x[1:5] returns everything
between index 1 through, but NOT including, index 5.  See

https://docs.python.org/3/tutorial/introduction.html#strings

which will give examples using strings.  A bit later the tutorial
addresses slicing in a list context.

BTW, the Python Tutorial is well worth reading in full!


-- 
boB
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to