tungwaiyip.info

home

about me

links

my software

Media

Yucatán Photos

St Lucia Photos

Photo Album

Videos

Blog

< June 2005 >
SuMoTuWeThFrSa
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
2627282930  

past articles »

Click for San Francisco, California Forecast

San Francisco, USA

 

Python's half open index notation

Beginner programmers often wonder about Python's sequence indexing and slicing notation. Array index starts from 0. Slicing uses half open notations, where L[a:b] is a subsequence with index x where a <= x < b.

Why is the endpoint excluded? Isn't it more intuitive if array index starts from 1 and the endpoint is included, so that a 3 elements array is referenced as L[1:3] with items L[1], L[2], L[3]?

It turns out this notation is an elegant and deliberate design and it has some excellent properties.

We write programs to operate on arrays, to find their length, traverse the subsequences, split them or join them. The half open notation always show a simple pattern. But the inclusive notation often requires adding 1 or substracting 1 to the indexes in many operations. Thus it is more vulnerable to off-by-one-error. This article One True Way of array indexing discuss this at length. I have reproduce its example (with corrections) below:

Operation Half open Inclusive
length of a slice L[a:b] b-a (b-a+1)
first n characters of L L[:n] L[1:n]
last n characters of L L[-n:] L[len(L)-n+1:]
The identity slice L == L[0:len(L)] == L[:] L[1:len(L)]
The empty slice L[a:a] is empty for any a. perhaps L[a:a-1]?
A slice of length n, from point a L[a:a+n] L[a:a+n-1]
Split L[a:b] at index c L[a:b] == L[a:c]+L[c:b] L[a:c-1]+L[c:b]

Another important property is an empty sequence can be expressed by L[a:a], while there is no natural way to express an empty sequence with the inclusive notation. But do we really need to care about a special case? Absolutely! In fact failure to account for empty input is one of the most common error. Just like zero is a fundamental concept in mathematics, always think how you program can handle null input. An inferior approach is to represent empty sequence by None or null pointer. This creates a special case so that a variable need to be tested before dereferencing. Failure to do so contributes to unexpected exceptions. It is an elegant design that L[a:b] can also represent sequences with 0 length.

C++'s STL also choose this notation to represent a range. According to the literature this is crucial because "algorithms that operate on n things frequently require n+1 positions. Linear search, for example (find) must be able to return some value to indicate that the search was unsuccessful." I have seen so many people flunked link list or data structure exercises because they have trouble dealing with the end of a list. Often a good solution is shift the focus beyond the n concrete objects to the n+1 positions around them. I hope this help to make sense of the half open notation.

2005.06.16 [, ] - comments

 

 

blog comments powered by Disqus

past articles »

 

BBC News

 

Iraqi Kurds issue warning to PKK (01 Aug 2015)

 

IAAF files show 'extent of cheating' (01 Aug 2015)

 

Outburst mars Bobbi Brown memorial (01 Aug 2015)

 

New US rights march starts in Selma (01 Aug 2015)

 

Dozens die as rains lash SE Asia (01 Aug 2015)

 

Snoop Dogg cash seized in Italy (01 Aug 2015)

 

New Taliban leader calls for unity (01 Aug 2015)

 

Aeroplane debris arrives in France (01 Aug 2015)

 

Bin Laden relatives on crash plane (01 Aug 2015)

 

Harman calls for Calais compensation (01 Aug 2015)

more »

 

Slashdot News for nerds, stuff that matters

 

Answering Elon Musk On the Dangers of Artificial Intelligence (2015-08-01T23:02:00+00:00)

 

Facebook's Slender 'Aquila' Drone To Provide Internet In Remote Areas (2015-08-01T22:09:00+00:00)

 

Ask Slashdot: Can You Disable Windows 10's Privacy-Invading Features? (2015-08-01T21:10:00+00:00)

 

ARIN IPv4 Addresses Run Out Tomorrow (2015-08-01T20:12:00+00:00)

 

In Windows 10, Ad-Free Solitaire Will Cost You -- Every Year (2015-08-01T19:16:00+00:00)

 

Researchers Find That Queen Bees Vaccinate Their Offspring (2015-08-01T18:15:00+00:00)

 

Microsoft Taps PBS To Advance Its National Talent Strategy With 'Code Trip' (2015-08-01T17:17:00+00:00)

 

ISPs Claim Title II Regulations Don't Apply To the Internet Because "Computers" (2015-08-01T16:17:00+00:00)

more »

 

TechPsychic Tech Rumors and Invented News

more »

 

SF Gate

 

Bay Area News (7 Jan 2012)

 

City Insider (11 Feb 2012)

 

Crime Scene (13 Feb 2012)

 

C.W Newius Column (10 Jan 2012)

 

C.W. Nevius Blog (11 Feb 2012)

 

Education News (10 Jan 2012)

 

KALW (11 Feb 2012)

 

Matier and Ross Blog (11 Feb 2012)

 

Daily Briefing, Aug. 2 (1 Aug 2015)

 

Netflix streamlines its DVD side (1 Aug 2015)

 

Facebook team creating ultimate “Super Mario” level (31 Jul 2015)

 

Daily Briefing, Aug. 1 (31 Jul 2015)

 

Diversifying tech: Google staff gives Midwesterners a jump-start (31 Jul 2015)

 

Yahoo buys Polyvore in fashionable move (31 Jul 2015)

more »

 

Asia Times Online

 

China ramps up charges against Zhou (Fri 20 Mar 2015 11:00:00 GMT)

 

'100 dead' in Myanmar fighting (Fri 20 Mar 2015 11:00:00 GMT)

 

Tunisian president vows no mercy (Fri 20 Mar 2015 11:00:00 GMT)

 

SPENGLER Israel's 'referendum' on 'two-state solution' (Fri 20 Mar 2015 11:00:00 GMT)

 

Russia, S Ossetia sign 'integration' pact (Fri 20 Mar 2015 11:00:00 GMT)

 

US military plunges Aquino into crisis (Fri 20 Mar 2015 11:00:00 GMT)

 

Rahmon celebrates Tajik democracy (Fri 20 Mar 2015 11:00:00 GMT)

 

THE BEAR'S LAIR Being old in 2040 no fun (Fri 20 Mar 2015 11:00:00 GMT)

 

China grant boosts Nepal ties (Fri 20 Mar 2015 11:00:00 GMT)

more »

 


Site feed Updated: 2015-Aug-01 17:00