How to create and use complex numbers
What are complex numbers
Complex numbers with a nonzero real component are written as "(real+imagj)
",
or can be created with the "complex(real, imag)
" function.
Complex numbers are written with a suffix of "j
" or "J
".
print 1j * 1J# from w w w. j av a2s . c om
print 1j * complex(0,1)
print 3+1j*3
print (3+1j)*3
print (1+2j)/(1+1j)
The code above generates the following result.
Complex Number Attributes
Attribute | Description |
---|---|
num.real | Real component of complex number num |
num.imag | Imaginary component of complex number num |
num.conjugate() Returns complex conjugate of num |
To extract these parts from a complex number z
,
use z.real
and z.imag
.
a=1.5+0.5j
print a.real
print a.imag
The code above generates the following result.