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 »

 

Kontagent

Kontagent is hiring software engineers

BBC News

 

Facebook shares see modest debut (18 May 2012)

 

Merkel 'suggests Greek euro vote' (18 May 2012)

 

Olympic relay flame lands in UK (18 May 2012)

 

Syria protests 'grip Aleppo city' (18 May 2012)

 

UN nuclear chief to visit Iran (18 May 2012)

 

Protesters angered by Gulf union (18 May 2012)

 

Malawi 'to overturn ban on gays' (18 May 2012)

 

Inside world's ‘priciest house’ (18 May 2012)

 

Maryland allows same-sex divorce (18 May 2012)

 

Silicon trick for next-gen memory (18 May 2012)

more »

 

Slashdot News for nerds, stuff that matters

 

Flashback Click Fraud Campaign Was a Bust (2012-05-18T22:58:00+00:00)

 

Microsoft Wins US Import Ban On Motorola's Android Devices (2012-05-18T22:38:00+00:00)

 

Facebook Privacy Suit Seeks Billion (2012-05-18T21:57:00+00:00)

 

Supervolcano Drilling Plan Gets Go-Ahead (2012-05-18T21:35:00+00:00)

 

EA To Provide Free Distribution To Kickstarter Games (2012-05-18T21:15:00+00:00)

 

Wozniak's Original System Description of the Apple ][ (2012-05-18T20:52:00+00:00)

 

Book Review: Elementary Information Security (2012-05-18T20:31:00+00:00)

 

How NASA and SpaceX Get Along Together (2012-05-18T19:50:00+00:00)

more »

 

TechPsychic Tech Rumors and Invented News

 

TechPsychic: AT&T: more money, says it's disruptive in funding from. (08 May 2010)

 

TechPsychic: I know that Apple is close to Apple Dominates, Hires ex-Googler - Yes, Android phones. (08 May 2010)

 

TechPsychic: AT&T says: Facebook Connect. (08 May 2010)

 

TechPsychic: Google's Nexus One of Google Chrome Release Adds Support subscriptions accounted for Amazon: Apple. (08 May 2010)

 

TechPsychic: Another stat: Twitter's Design of this is giving rise of BlackBerry Foursquare Map App store end. (07 May 2010)

 

TechPsychic: Like educational sales Up around Apple iPad makes money Plan costs half an Apple. (07 May 2010)

 

TechPsychic: Instead added extensions, social Networks than double, everyone jumps in Silicon Valley? (07 May 2010)

 

TechPsychic: So why iTunes App lets Social Networks Verizon Wireless Internet. (07 May 2010)

more »

 

SF Gate

 

Shares fall flat - then Facebook buys a company (2012-05-18T21:33:47PDT)

 

Gov. mistakenly says Facebook invented in Calif. (2012-05-18T21:33:47PDT)

 

Woman killed, 2 others hurt in Berkeley wreck (2012-05-18T21:33:47PDT)

 

Lawyer arrested in Dublin bicyclist's hit-run death (2012-05-18T21:33:47PDT)

 

9/11 families upset over ground zero museum delays (2012-05-18T21:33:47PDT)

 

Largest protests yet in Syrian city of Aleppo (2012-05-18T21:33:47PDT)

 

Video could damage Ross Mirkarimi's efforts (2012-05-18T21:33:47PDT)

 

Obama to tap Bay Area again for campaign cash (2012-05-18T21:33:47PDT)

 

Hewlett-Packard considers cutting 25,000 jobs (2012-05-18T21:41:17PDT)

 

Minute Maid wins 'Pomegranate Blueberry' case (2012-05-18T21:41:17PDT)

 

A look at how some IPO stocks have fared (2012-05-18T21:41:17PDT)

 

Delta details airfare search glitch (2012-05-18T21:41:17PDT)

 

Kraft Foods lowers prices on select coffee (2012-05-18T21:41:17PDT)

 

George Clinton, Black Eyed Peas settle song suit (2012-05-18T21:41:17PDT)

more »

 

Asia Times Online

 

Riddle of the Scarborough Shoals (18 May 2012)

 

THE ROVING EYE : NATO occupies sweet home Chicago (18 May 2012)

 

US Iran hawks in some disarray (18 May 2012)

 

Tehran: To talk or not to talk (18 May 2012)

 

The 'illogic' of China's North Korea policy (18 May 2012)

 

BOOK REVIEW : Cherry-picking from China's success (18 May 2012)

 

SPEAKING FREELY : Nepal's constitution: Respect the dissenters (18 May 2012)

 

China's start-ups hold global potential (18 May 2012)

 

US gives green light to investment in Myanmar (18 May 2012)

 

IT WORLD : Facebook floats (18 May 2012)

more »

 


Site feed Updated: 2012-May-18 16:00