1
import xlrd
book = xlrd.open_workbook("univ_list.xls")
sh = book.sheet_by_index(0)
for r in range(sh.nrows)[1:]: # line 4
    print sh.row(r)[:4] # line 5

What does [1:] mean in line 4? What does [:4] mean in line 5?

Terry Li
  • 16,870
  • 30
  • 89
  • 134
  • 5
    possible duplicate of [Good Primer for Python Slice Notation](http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation) – GWW Jan 11 '12 at 19:17
  • agreed ^^^^ this is answered above but without similar keywords – 0x1F602 Jan 11 '12 at 19:19

2 Answers2

5

Here's an example of what you're seeing on Wikipedia: http://en.wikipedia.org/wiki/Array_slicing#1991:_Python

It's called array slicing. [1:] gets all the items except for the first, and [:4] gets the first 4 items.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 1
    Good mnemonics for slicing can be found in Python docs, also mentioned here: http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation ("One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0.") – Roman Susi Jan 11 '12 at 19:37
0

[1:] means you just want to get the items from position 1 on in your list, string, etc.

[:4] means you want to get up to item 4 in your string or list.

Remember, numbering starts at 0.

so in f = 'apple', f[0]='a', f[1]='p', f[1:]='pple'

Read up on slice notation -- there's a lot more you can do with that.

maneesha
  • 685
  • 3
  • 11
  • 19