Learnitweb

Complex number in Python

Complex numbers are used in scientific calculations. Complex number in Python is represented in the form of a + bj.

Here, a and b are real numbers and j represents the imaginary unit, satisfying the equation j2 = -1.

Because no real number satisfies this equation, j is called an imaginary number. For the complex number a + bj, a is called the real part, and b is called the imaginary part.

Note

  • You have to use letter j to represent imaginary number. You can not use any other letter to represent imaginary number. However, case of j is not important, so you can use J or j.
  • Real part of the complex number can be represented as decimal, binary, octal or hexadecimal form. But imaginary part can only be represented as decimal.

Complex Numbers Mathematical Calculations

Complex numbers support mathematical calculations such as addition, subtraction, multiplication and division.

num=6+7j
num2=1+2j
print('type of num is ', type(num))
print('real part of complex number is', num.real)
print('imaginary part of complex number is', num.imag)
print('sum of two complex numbers is', num+num2)
print('difference of two complex numbers is', num-num2)
print('product of two complex numbers is', num*num2)
print('division of two complex numbers', num/num2)

Output

type of num is  <class 'complex'>
real part of complex number is 6.0
imaginary part of complex number is 7.0
sum of two complex numbers is (7+9j)
difference of two complex numbers is (5+5j)