Dumb python questions
Paul Rubin
phr-n2001 at nightsong.com
Wed Aug 15 05:58:26 EDT 2001
Richard Jones <richard at bizarsoftware.com.au> writes:
> It's right there in the second paragraph...
>
> '''Complex numbers have a real and imaginary part, which are both implemented
> using double in C. To extract these parts from a complex number z, use z.real
> and z.imag.'''
Oops, yes, there it is. Thanks. It's not in the table of operations
underneath and probably ought to be added.
>
> > Here's another dumb question, about the for loop: if I want to add
> > up the first 100 numbers in the naive way, am I really supposed to say
> >
> > sum = 0
> > for i in range(101) :
> > sum = sum + i
> >
> > and if I say that, will range(101) really malloc up and populate an
> > array of length 100?
>
> You could use xrange, which won't malloc the list.
xrange apparently doesn't malloc the values until they're used, but
they eventually get malloc'd. I think functional programming has run
slightly amuck here. What's wanted is something equivalent to
sum = 0
i = 1
while i <= 100 :
sum = sum + i
i = i + 1
Python is really pleasant to code in and mostly well designed. I just
keep hitting what seem like little lapses of common sense.
Here's another one: the print statement puts a gratuituous space after
each thing you print. So
for i in ['a','b','c']:
print i,
prints "a b c " instead of "abc".
More information about the Python-list
mailing list