[Tutor] problem reading script
Martin A. Brown
martin at linux-ip.net
Fri Jul 1 11:51:20 CEST 2011
Greetings Lisi,
: I am supposed to be looking at scripts on-line, reading them and
: making sure that I understand them.
When you see an example that you don't understand, consider trying
to do something similar in an interactive Python interpreter. This
is a simple way to learn in Python.
: I think taht most of teh things I can't make more than a guess
: at, are modules taht I don't know, and I can mostly make them
: out. But the unpaired double quotation mark, " , in the
: following has me stumped:
Look carefully. It is not an unpaired double-quotation mark. Are
you using a monospaced font to read code? If you are using a
variable width font, you should change to monospaced. Save yourself
some future headache. Really. Use a monospaced font.
: report['BzrLogTail'] = ''.join(bzr_log_tail)
Do you have a Python interpreter handy? This is a fairly typical
pythonic expression for concatenating elements of a list into a
string. Try the following in a python interpreter:
>>> l = list()
>>> l.append("a")
>>> l.append("b")
>>> l.append("c")
>>> l
['a', 'b', 'c']
>>> ''.join(l)
'abc'
Now, you are probably wondering about that peculiar looking syntax
for calling join() on a list. This single quoted empty string '' is
still a string, so it has methods that can be called on it. Read up
on methods available on strings [0] to get a better idea. For other
examples of using the string join() method, consider the following:
>>> ':'.join(l)
'a:b:c'
>>> vampire = [ 'The','Deluxe','Transitive','Vampire' ]
>>> ' '.join(vampire)
'The Deluxe Transitive Vampire'
And, for something just a bit fancier, make a list of ints, using
the function called range():
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Convert them to a list of strings:
>>> [str(x) for x in range(10)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Now, concatenate them and separate with some spacedash!
>>> visual_separator = ' -- '
>>> visual_separator.join(str(x) for x in range(10))
'0 -- 1 -- 2 -- 3 -- 4 -- 5 -- 6 -- 7 -- 8 -- 9'
With any luck, these examples help explain what you were reading.
-Martin
[0] http://docs.python.org/dev/library/stdtypes.html#string-methods
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list