Complex numbers are a built-in numeric data type in Python used to represent values that consist of a real part and an imaginary part. It is made of two distinct parts:
- Real Part: A standard integer or floating-point number.
- Imaginary Part: A number followed by the letter
jorJ.
Defining a Complex Number
To creat a complex number the real part and imaginary part are combined using the + or - operator. The syntax is represented as a + bj.
How to use the "j"
In mathematics, the imaginary unit is often represented by . However, in Python, the suffix j or J is mandatory.
- Correct: z = 2 + 4j
- Incorrect: z = 2 + 4i (This will result in a
SyntaxError)
The Coefficient
The imaginary part must consist a coefficient, this mean that we cannot simply write j or J alone, it must be represented with a coefficient even if the coefficient is 1, it must be explicitly written as 1j.
- Correct:
z = 1j - Incorrect:
z = j(This will result in aNameError)
The complex() Constructor
There is a built-in function called complex() in python used to initailize complex numbers. This is useful when creating numbers from variables or when the real and imaginary parts are determined dynamically complex(real, imaginary).
Here are some behaviors to look out for:
- If both arguments are omitted, the function returns
0j. - The arguments can be integers or floats, but the resulting real and imaginary components are always stored as floats.
Accessing Attributes
Once a complex number is defined, its individual components can be retrieved using two built-in attributes: .real and .imag.
Attributes:
.real: Returns the real component of the complex number as a float..imag: Returns the imaginary component of the complex number as a float.
The conjugate() Method
The .conjugate() method is a built-in function that returns a new complex number with the sign of the imaginary part reversed.
Mathematical Representation:
If a complex number is defined as , the conjugate is defined as:
Operations and Constraints
Complex numbers in Python support standard arithmetic operations but are restricted by certain mathematical constraints.
Basic Arithmetic
You can perform addition, subtraction, multiplication, and division between complex numbers. Python handles these operations according to the laws of complex algebra.
Operations Table:
| Operation | Syntax | Logic |
|---|---|---|
| Addition | z1 + z2 | (a+c)+(b+d)j(a+c)+(b+d)j |
| Subtraction | z1 - z2 | (a−c)+(b−d)j(a−c)+(b−d)j |
| Multiplication | z1 * z2 | (ac−bd)+(ad+bc)j(ac−bd)+(ad+bc)j |
| Division | z1 / z2 | Calculated via the conjugate of the denominator |
Comparison Constraints
Unlike integers and floats, complex numbers do not have a natural order. Consequently, they cannot be compared using magnitude-based operators.
Constraint:
In Python, the following operators will raise a TypeError when used with complex numbers:
- Less than (
<) - Greater than (
>) - Less than or equal to (
<=) - Greater than or equal to (
>=)
Equality Check:
Only the equality (==) and inequality (!=) operators are valid for complex numbers. Two complex numbers are equal only if their real parts and imaginary parts are both identical.
