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

 

Anger over Russian convoy in Ukraine (22 Aug 2014)

 

Dozens killed in Iraq mosque attack (22 Aug 2014)

 

Israel vows response to boy's death (22 Aug 2014)

 

Woman 'saw Congo colonel order rape' (22 Aug 2014)

 

Syria death toll 'more than 191,000' (22 Aug 2014)

 

Malaysia observes silence for MH17 (22 Aug 2014)

 

German policeman in 'cannibal' trial (22 Aug 2014)

 

Rain hampers Japan landslide rescue (22 Aug 2014)

 

Tour buses collide near Egypt resort (22 Aug 2014)

 

Fed says 'slack' in US jobs market (22 Aug 2014)

more »

 

Slashdot News for nerds, stuff that matters

 

Researchers Hack Gmail With 92 Percent Success Rate (2014-08-22T20:46:00Z)

 

A Better Way To Make Mind-Controlled Prosthetic Limbs (2014-08-22T20:04:00Z)

 

When Customer Dissatisfaction Is a Tech Business Model (2014-08-22T19:22:00Z)

 

Apple CarPlay Rollout Delayed By Some Carmakers (2014-08-22T18:38:00Z)

 

A Movie of Triton Made From Voyager 2's Fly-by 25 Years Ago (2014-08-22T17:51:00Z)

 

Students From States With Faster Internet Tend To Have Higher Test Scores (2014-08-22T17:04:00Z)

 

What's After Big Data? (2014-08-22T16:23:00Z)

 

Researchers Made a Fake Social Network To Infiltrate China's Internet Censors (2014-08-22T15:36:00Z)

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)

 

Airbnb squatters leave Palm Springs condo (22 Aug 2014)

 

Toms sells 50% stake to Bain Capital to fund sales expansion (22 Aug 2014)

 

Gap profit tops estimates as Old Navy boosts sales (22 Aug 2014)

 

Online report of possible PayPal spinoff boosts eBay stock (22 Aug 2014)

 

Craig Menear will become Home Depot CEO (22 Aug 2014)

 

Atlantic City casino workers face joblessness, turn to prayer (22 Aug 2014)

more »

 

Asia Times Online

 

Why Washington's war on terror failed (Fri 22 Aug 2014 11:00:00 GMT)

 

THE ROVING EYE The killer on the (Saudi) king's highway (Fri 22 Aug 2014 11:00:00 GMT)

 

ISIS: Caliphate or pretenders? (Fri 22 Aug 2014 11:00:00 GMT)

 

Delicate diplomacy for Vietnam (Fri 22 Aug 2014 11:00:00 GMT)

 

'Paramilitarizing' the South China Sea (Fri 22 Aug 2014 11:00:00 GMT)

 

SINOGRAPH Simon Leys: An appreciation (Fri 22 Aug 2014 11:00:00 GMT)

 

Sunni Awakening in Iraq and the future of IS (Fri 22 Aug 2014 11:00:00 GMT)

 

Sanctions rebound to hit Europeans (Fri 22 Aug 2014 11:00:00 GMT)

 

Rights before weapons for Vietnam (Fri 22 Aug 2014 11:00:00 GMT)

 

The American cult of bombing (Fri 22 Aug 2014 11:00:00 GMT)

 

Laos 'halts work' on Don Sahong dam (Fri 22 Aug 2014 11:00:00 GMT)

 

China still backing Lao high-speed train (Fri 22 Aug 2014 11:00:00 GMT)

more »

 


Site feed Updated: 2014-Aug-22 14:00