[Tutor] abs(z)

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Dec 7 18:46:42 EST 2003


> >>> a=3.0+4.0j

First you are working with "complex numbers" or "imaginary
numbers"
THere are used to represent(among other things) 2D vectors.
The first number is the real part and represents the vector
segment in one dimension and the imaginary part(with a j)
represents the segment in a perpendicular plane

> >>> a.real
> 3.0
> >>> a.imag
> 4.0

> >>> abs(a)  # sqrt(a.real**2 + a.imag**2)
> 5.0

So abs() gets the absolute length of the vector which,
using Pythagorus' theorem, is the square root(sqrt())
of the sum of the squares(**2) of the other two sides.

[ In case you are curious about what imaginary numbers are
used for, they crop up a lot in engineering and scientific
calculations. For example they can be used to represent the
electrical current and/or voltage in a complex electronic
circuit. ]

> comment text after hash on same physical line doesn't
> do anythin to the program??

The interpreter doesn't do anything with it, it is literally
just a comment to you and other programmers to explain what's
going on.

HTH,

Alan G.




More information about the Tutor mailing list