Content · Programming · Python

Python cmath.rect() Function: Usage and Examples

Build a complex number from a radius and phase angle.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Build a complex number from a radius and phase angle.

What It Does and Why It Matters


rect(radius, phase) returns a complex value whose real and imaginary components are based on cosine and sine; the phase is in radians.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

import cmath
print(cmath.rect(2, 0))

How the Code Works


  • The imports make these standard-library tools available: cmath.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.

Expected output:

(2+0j)

For the demonstrated inputs, Python displays '(2+0j)'. This result follows from the operation described above; change one input at a time to understand which rule matters.

Second Example: Compare Two Cases


Use a different input or a related operation to observe an important difference. The two examples help separate the general rule from one particular result.

from decimal import Decimal
print(Decimal("0.10")+Decimal("0.20"))

Expected output:

0.30

Where You Would Use This


Use this in calculations, data summaries, simulations or validation when the numerical assumptions match the task. Check input domains, units and precision before interpreting a result. A single example output does not prove a statistical conclusion.

Practice Exercise


Round-trip a complex value through polar() and rect().

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Passing degrees as the phase argument without conversion.
  • Using a numeric result without checking empty data, invalid ranges, precision and the meaning of its units.

Key Takeaways


  • Build a complex number from a radius and phase angle.
  • rect(radius, phase) returns a complex value whose real and imaginary components are based on cosine and sine; the phase is in radians.
  • Round-trip a complex value through polar() and rect().
Share this resourceSend this page to someone who may find it useful.