Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In Python, complex numbers are represented using the complex
data type. A complex number consists of a real part and an imaginary part, written as a + bj
, where a
and b
are real numbers and j
is the imaginary unit (the square root of -1).
Here is a step-by-step tutorial on how to work with complex numbers in Python:
Creating complex numbers:
You can create a complex number in different ways:
Using the complex()
constructor:
z1 = complex(3, 4) # 3 + 4j print(z1) # Output: (3+4j)
Using the literal notation:
z2 = 3 + 4j print(z2) # Output: (3+4j)
Accessing the real and imaginary parts:
You can access the real and imaginary parts of a complex number using the real
and imag
attributes:
z = 3 + 4j real_part = z.real imag_part = z.imag print(real_part) # Output: 3.0 print(imag_part) # Output: 4.0
Complex arithmetic:
Python supports basic arithmetic operations for complex numbers, including addition, subtraction, multiplication, and division:
z1 = 3 + 4j z2 = 1 - 2j z_add = z1 + z2 z_sub = z1 - z2 z_mul = z1 * z2 z_div = z1 / z2 print(z_add) # Output: (4+2j) print(z_sub) # Output: (2+6j) print(z_mul) # Output: (11+2j) print(z_div) # Output: (-1+2j)
Complex conjugate:
You can find the complex conjugate of a complex number (i.e., the number with the same real part and the opposite imaginary part) using the conjugate()
method:
z = 3 + 4j z_conjugate = z.conjugate() print(z_conjugate) # Output: (3-4j)
Modulus (absolute value) and phase (angle):
To calculate the modulus (absolute value) and phase (angle) of a complex number, you can use the built-in abs()
function and the cmath.phase()
function from the cmath
module:
import cmath z = 3 + 4j modulus = abs(z) phase = cmath.phase(z) print(modulus) # Output: 5.0 print(phase) # Output: 0.9272952180016122 (in radians)
Polar coordinates conversion:
You can convert between polar coordinates using the cmath.polar()
function from the cmath
module:
import cmath # Rectangular to polar coordinates z = 3 + 4j polar_coordinates = cmath.polar(z) print(polar_coordinates) # Output: (5.0, 0.9272952180016122)
Creating and Working with Complex Numbers in Python:
complex()
constructor to create complex numbers and perform basic operations.# Example complex_num = complex(3, 4) result = complex_num + 2j
Operations and Methods for Complex Numbers in Python:
conjugate()
on complex numbers.# Example complex1 = 2 + 3j complex2 = 1 - 2j sum_result = complex1 + complex2 conjugate_result = complex1.conjugate()
Complex Literals and Constants in Python:
# Example complex_literal = 1 + 2j zero_complex = 0j
Converting Between Complex and Other Data Types in Python:
# Example complex_num = complex(2, 3) int_part = int(complex_num.real)
Common Mathematical Operations with Complex Numbers in Python:
# Example complex_num = 1 + 2j squared_result = complex_num ** 2 magnitude = abs(complex_num)
Applications of Complex Numbers in Python Programming:
# Example: Signal processing application import cmath frequency = 1000 # Frequency in Hertz time = 0.001 # Time in seconds angular_frequency = 2 * cmath.pi * frequency signal = cmath.exp(1j * angular_frequency * time)
Handling Complex Data in Scientific Computing with Python:
# Example: Quantum mechanics simulation import numpy as np theta = np.pi / 4 quantum_state = np.array([cmath.exp(1j * theta / 2), cmath.exp(-1j * theta / 2)])
Python Complex Type vs Other Numeric Types:
# Example result = 3 + 4j * 2 # Complex number interacts with an integer