Python has native syntax for complex numbers: writing 3 + 4j creates one directly, using j instead of the mathematical i, following the electrical-engineering convention, and the type supports the usual arithmetic operators, plus .real and .imag attributes to pull out each component. abs() on a complex number returns its magnitude rather than a sign-stripped value, since complex numbers aren't ordered.
1Understanding Complex Numbers
Python has native syntax for complex numbers: writing 3 + 4j creates one directly, using j instead of the mathematical i, following the electrical-engineering convention, and the type supports the usual arithmetic operators, plus .real and .imag attributes to pull out each component. abs() on a complex number returns its magnitude rather than a sign-stripped value, since complex numbers aren't ordered.
Complex numbers rarely show up in everyday scripting — they're mainly used in scientific/engineering code, like signal processing, electrical engineering, and certain numerical algorithms, often via libraries like NumPy, which builds on this same complex type.
z = 3 + 4j
print(z.real, z.imag)
print(abs(z))2Practical Example
Here is a real-world application of Complex Numbers showing how it is used in production Python code.
z1 = 2 + 3j
z2 = 1 - 1j
print(z1 + z2)
print(z1 * z2)3Best Practices
Follow these guidelines when working with Complex Numbers:
1. Use the built-in complex type or complex() constructor instead of modeling real/imaginary parts as a manual tuple
2. Access .real and .imag instead of parsing the number's string representation
3. Reach for NumPy's complex arrays instead of Python's plain complex type when doing bulk numerical work
Tip: Complex numbers rarely show up in everyday scripting — they're mainly used in scientific/engineering code, like signal processing, electrical engineering, and certain numerical algorithms, often via libraries like NumPy, which builds on this same complex type.
z = 3 + 4j
print(z.real, z.imag)
print(abs(z))