[Tutor] Language truce
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Thu, 28 Jun 2001 14:20:58 -0700 (PDT)
On Thu, 28 Jun 2001, Michael Powe wrote:
> >>>>> "Danny" == Danny Yoo <dyoo@hkn.eecs.berkeley.edu> writes:
>
> Danny> On Thu, 28 Jun 2001, Michael Powe wrote:
>
> >> A couple things I mentioned, like you can't increment through
> >> loops.
>
> Danny> I'm a little confused by what you mean by "incrementing
> Danny> through loops". Can you give an example of what you mean?
>
> The standard C (for i=0; i < n; i++), which uses the increment
> operator (i++) which is not present in python. I'm sure there must be
> some discussion somewhere about why the decision was made to leave
> this out, I just have not come across it.
>
> Yes, as someone else points out, you can use range(), but obviously
> that is not the same thing. (In some cases, it may be functionally
You're right that converting C's:
for (i = 0; i < n ; i++) { ... }
isn't quite the same as Python's
for i in range(n): ...
but it comes pretty close --- the extra thing that Python is doing is
actually constructing a list 'n' elements wide before going looping.
One reason that 'for/in' might be preferred is because we are guaranteed
that the loop will always terminate: there can only be a finite number of
elements in a list. That's one big advantage of a loop construct that
goes along elements --- theoretically, we'll never get into infinite
loops.
On the other hand, there's a little bit of inefficiency due to actually
constructing that n-element list. If we're just constructing the list,
and then just picking out the numbers in order "1...2...3...", it seems a
little wasteful to make that whole list.
In this case, Python provides an "xrange" function ("extended" range).
It looks very much like range(), but it actually doesn't construct a
list. So:
for i in xrange(n):
works exactly like the C equivalent, without the messy syntax... *grin*
> equivalent and maybe that is the point.) Also, maybe it's just me,
> but I consider
>
> k = k+1
>
> as a loop counter to just be unacceptably clumsy. Forcing me to use
> that construct tells me that I should not be designing my program that
> way.
Very true, but there are many ways to avoid the explicit maintenance of a
loop counter. range/xrange is one way to avoid loop counters. Another
function that Python provides is called the 'map' function. map is very
neat, but we'll need an example to shows why:
###
>>> messy_strings = [' this string has leading spaces ',
... 'and trailing spaces too ',
... ' we can use map to strip all the strings ',
... 'in one shot!']
>>> import string
>>> clean_strings = map(string.strip, messy_strings)
>>> clean_strings
['this string has leading spaces', 'and trailing spaces too',
'we can use map to strip all the strings', 'in one shot!']
###
The cleanest code is that which doesn't exist. *grin*
> My 'teach-yourself-python' project is an address book. I happen to
> need one and I don't like anything that's already available.
> Unfortunately, the python books just aren't making it as instructional
> tools. I know quite a bit about python syntax &c and practically
> nothing about actually writing programs in python. Anyway, I have the
> data entry part and the write-to-file parts completed -- that was
> easy. But, the file-reading and data storage parts are more
> interesting. In C, I would just use a struct and a linked list and
> write/read the structs to/from a binary file -- I've done all that
> type of stuff before.
It's nice to see a fellow C programmer here! Yeah, it will take a while
to get used to Python's way of doing things, but it's very much worth it
--- I've found that my own C programs look a lot better after learning
Python.
To save your address book to disk, you might be interested in the 'pickle'
module: it handles writing structures directly to disk and back, so it
makes saving most things ridiculously easy.
http://python.org/doc/lib/module-pickle.html
We're here, so if you have any questions, feel free to ask them. If you
want to express your questions in C terms, that's ok too.
Good luck!