python list index - an easy question

alister alister.ware at ntlworld.com
Sun Dec 18 03:12:37 EST 2016


On Sat, 17 Dec 2016 11:10:22 -0800, John 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]?
> 
>    Thanks!!
> 
> John

as well as al the other excellent & detailed explanations
think of the slice working on the gaps between the elements (the ',') & 
not the element itself




-- 
In the long run we are all dead.
		-- John Maynard Keynes


More information about the Python-list mailing list