Chris wrote:
On Jun 13, 9:38 am, Phillip B Oldham <[EMAIL PROTECTED]> wrote:
Thanks guys. Those comments are really helpful. The odd semi-colon is
my PHP background. Will probably be a hard habbit to break, that
one! ;) If I do accidentally drop a semi-colon at the end of the line,
will that cause any weird errors?
Also, Chris, can you explain this:
a, b = line.split(': ')[:2]
I understand the first section, but I've not seen [:2] before.
That's slicing at work. What it is doing is only taking the first two
elements of the list that is built by the line.split.
slicing is a very handy feature... I'll expand on it a little
OK so, first I'll create a sequence of integers
>>> seq = range(10)
>>> seq
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"take element with index 4 and everything after it"
>>> seq[4:]
[4, 5, 6, 7, 8, 9]
"take everything up to, but not including, the element with index 4"
>>> seq[:4]
[0, 1, 2, 3]
"take the element with index 3 and everything up to, but not including,
the element with index 6"
>>> seq[3:6]
[3, 4, 5]
then there's the step argument
"take every second element from the whole sequence"
>>> seq[::2]
[0, 2, 4, 6, 8]
"take every second element from the element with index 2 up to, but not
including, the element with index 8"
>>> seq[2:8:2]
[2, 4, 6]
Hope that helps.
--
http://mail.python.org/mailman/listinfo/python-list