A complex number is a data type in Python used to represent numbers with both a real part and an imaginary part. This data type is primarily used in scientific, engineering, and mathematical applications where complex number arithmetic is required.
In Python, a complex number is written in the form a + bj, where:
- a is the real part.
- b is the imaginary part.
- j is the imaginary unit (equivalent to i in standard mathematics), representing the square root of -1.
Operations Possible on Complex Numbers
Complex numbers support standard arithmetic operations.
- Arithmetic Operations: You can perform addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**) with complex numbers. The operations follow the standard rules of complex arithmetic.
- Comparison Operations: Unlike integers and floats, you cannot use ordering comparisons like <, >, <=, or >= with complex numbers. This is because there is no universally agreed-upon way to order them. You can only check for equality (==) and inequality (!=).
Attributes and Methods
Complex number objects in Python have specific attributes to access their components and methods for common operations.
- .real attribute: Returns the real part of the complex number as a float.
- .imag attribute: Returns the imaginary part of the complex number as a float.
- .conjugate() method: Returns the complex conjugate of the number. The conjugate of a + bj is a - bj.
The cmath Module
For more advanced mathematical functions involving complex numbers (like square roots, logarithms, or trigonometric functions), Python provides a dedicated module called cmath.
- cmath.phase(): Returns the phase (or angle) of a complex number.
- cmath.polar(): Returns the representation of a complex number in polar coordinates (radius and phase).
- cmath.sqrt(): Calculates the square root of a complex number.
# --- 1. Import the cmath module for advanced functions ---
import cmath
# --- 2. Creating Complex Numbers ---
# A complex number is defined by a real part and an imaginary part (ending with 'j').
c1 = 3 + 4j
c2 = 1 - 2j
print("--- Creating Complex Numbers ---")
print(f"c1 = {c1}")
print(f"Type of c1: {type(c1)}")
print(f"c2 = {c2}")
# --- 3. Accessing Real and Imaginary Parts ---
# You can access the real and imaginary parts using .real and .imag attributes.
print("\n--- Accessing Parts ---")
print(f"Real part of c1: {c1.real}")
print(f"Imaginary part of c1: {c1.imag}")
# --- 4. Arithmetic Operations ---
# Complex numbers support standard arithmetic operations.
print("\n--- Arithmetic Operations ---")
print(f"Addition (c1 + c2): {c1 + c2}")
print(f"Subtraction (c1 - c2): {c1 - c2}")
print(f"Multiplication (c1 * c2): {c1 * c2}")
print(f"Division (c1 / c2): {c1 / c2}")
print(f"Exponentiation (c1 ** 2): {c1 ** 2}")
# --- 5. Comparison Operations ---
# You can only check for equality (==) and inequality (!=).
# Ordering comparisons (<, >, <=, >=) are not supported and will raise an error.
c3 = 3 + 4j
print("\n--- Comparison Operations ---")
print(f"Is c1 equal to c2? (c1 == c2): {c1 == c2}")
print(f"Is c1 equal to c3? (c1 == c3): {c1 == c3}")
print(f"Is c1 not equal to c2? (c1 != c2): {c1 != c2}")
# --- 6. Complex Number Methods ---
# The most common method is .conjugate().
print("\n--- Complex Methods ---")
print(f"Original number (c1): {c1}")
print(f"Conjugate of c1: {c1.conjugate()}")
# --- 7. Built-in and cmath Functions ---
# The built-in abs() function gives the magnitude (or modulus) of a complex number.
# Magnitude of a + bj is sqrt(a^2 + b^2)
print("\n--- Built-in and cmath Functions ---")
print(f"Magnitude of c1 (abs(c1)): {abs(c1)}") # sqrt(3^2 + 4^2) = sqrt(25) = 5.0
# The cmath module provides more advanced functions.
# Phase (angle) of the complex number in radians.
print(f"Phase of c1 (cmath.phase(c1)): {cmath.phase(c1):.4f} radians")
# Polar coordinates (radius, phase)
radius, phase = cmath.polar(c1)
print(f"Polar representation of c1: (radius={radius:.1f}, phase={phase:.4f})")
# Square root
print(f"Square root of c1 (cmath.sqrt(c1)): {cmath.sqrt(c1)}")