Dumb python questions

Richard Jones richard at bizarsoftware.com.au
Wed Aug 15 04:17:05 EDT 2001


On Wednesday 15 August 2001 17:59, Paul Rubin wrote:
> kosh <kosh at aesaeion.com> writes:
> > > 1) Suppose I have a complex number like a = 3+4j.  How do I get the
> > > real and imaginary parts separately?
> > >
> > >>> a = 3+4j
> > >>> dir(a)
> >
> > ['conjugate', 'imag', 'real']
> >
> > >>> a.imag
>
> Thanks.  Can you tell me if that's documented anywhere?
>
>   http://www.python.org/doc/current/lib/typesnumeric.html
>
> describes the conjugate() operation but not the real or imag members.
> I'd like to report that as a deficiency in the documentation.

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.'''


> 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.


    Richard




More information about the Python-list mailing list