Imagining Complex Numbers

Complex numbers have a real and imaginary part, both of which are floats.

TECHNICAL A complex numeric literal is the sum of a real part and an imaginary STUFF part.

In Python, to specify an imaginary number, or the imaginary part of a complex number, append j to the number.

To separate the real or imaginary part of a complex number, type x.real for the real part or x.imag for the imaginary part, like this:

>>>

x

=(3+6j)

>>>

x.

real

3.0

>>>

x.

, imag

6.0

Augmented assignment

Use augmented assignment operators to perform actions such as addition, concatenation, subtraction, and multiplication on an object and store the result using the same name. (An "augmented" assignment statement has an extra feature—an addition or concatenation operation—built in.) Here's why:

• Augmented assignment statements are easier to debug.

The statement counter +=1 clearly uses only one object. If you use the statement counter = counter + 1, you must read the statement carefully to make sure the two objects are the same. If you're trying to debug someone else's thousand-line program, you'll appreciate the simplicity!

• Augmented assignment statements are more efficient.

These statements evaluate an object only once and change mutable objects in place; regular assignment evaluates an object twice and creates a new copy in the process. This can slow performance with large objects.

These are the most commonly used augmented assignment operators:

+= (addition or concatenation):

Was this article helpful?

0 0

Post a comment