liveBook · Manning

Complex floating-point type

As explained in The Quick Python Book, Fourth Edition

A complex number is a number consisting of a real component and an imaginary component, represented in Python with an imaginary component suffixed by j.

A complex number combines a real component with an imaginary component. Python writes the imaginary component with a trailing j, as in 3 + 2j or 4.2 + 6.3j. section 3.2.1 section 4.7.4

Use complex values through ordinary arithmetic and component access:

  • Write an expression such as 3 + 2j; Python creates one complex-number object and displays it in parentheses. section 4.7.4
  • Apply addition, subtraction, multiplication, division, exponentiation, and related arithmetic operators to complex values. section 3.2.1 section 4.7.4
  • Read z.real and z.imag to obtain the components; both attributes return floating-point numbers. section 4.7.4
  • Use cmath for functions such as square root, because math functions do not apply to complex numbers. section 4.7.5

Inspecting a computed value

z = (3 + 5j)\nz.real\n3.0\nz.imag\n5.0\n\nMultiplication preserves the complex type: (1 + 2j) * (3 + 4j) produces (-5 + 10j). section 4.7.4

import cmath
cmath.sqrt(-1)
 
1j

Try cmath.sqrt(-1) to see a complex result produced by a dedicated complex-number function. section 4.7.5

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains literal syntax, arithmetic, component attributes, object preservation, and cmath usage without formal mathematical treatment.
Examples
Demonstrates literals, multiplication, exponentiation, real and imaginary attributes, and cmath.sqrt(-1).
What it assumes
Assumes familiarity with standard numeric operations while introducing Python’s j syntax, complex objects, and cmath selection.
What you can do after
Apply complex arithmetic and inspect components, then call complex-capable functions through the cmath module.
How it connects
Ties complex arithmetic to numeric types, arithmetic operators, object attributes, modules, and the cmath module.

Also explained in

Compare how 3 books explain complex floating-point type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.