Complex numbers have a real part and an imaginary part. Python includes a built-in complex type for mathematical and engineering tasks.
Understanding the Concept
A complex number is written using j for the imaginary component, such as 3 + 4j. Python supports addition, subtraction, multiplication, and division with complex values. Complex numbers are useful in signal processing, electrical engineering, and certain mathematical calculations. They cannot be directly ordered using < or > because complex numbers do not have a natural linear ordering. The real and imag attributes expose each component.
When and Why You Would Use This
You do not need complex numbers for most beginner scripts, but recognizing the type helps when you encounter mathematics-focused examples later in the course.
Practical Example
first_signal = 3 + 4j
second_signal = 1 - 2j
combined_signal = first_signal + second_signal
print(combined_signal)
print(first_signal.real, first_signal.imag)
Understanding the Example
The combined value is (4+2j). The last line prints 3.0 and 4.0 for the real and imaginary parts of the first value. The letter j is part of Python’s numeric literal syntax here.
Example Output
(4+2j)
3.0 4.0
Try It Yourself
Create two complex values, add them, and inspect the real and imaginary components of the result.
Common Beginner Mistakes
- Using i instead of j inside a complex literal without defining it as a variable.
- Attempting to compare complex numbers with < or >.
- Assuming the imaginary component is just a string because the literal contains a letter.
Key Takeaways
- complex holds real and imaginary components.
- Python uses j in complex numeric literals.
- Complex numbers support arithmetic but not ordinary ordering comparisons.