Python Complex Number

About Python Complex Number

In Python, a complex number is a number that comprises a real part and an imaginary part. The imaginary part is expressed with a suffix of 'j'.

4 + 5j is an example of complex number where 4 is the real part and 5j is the imaginary part.

Creating Complex Number in Python

You can define a complex number in Python by simply assigning a number part and imaginary part to a variable.

In the following example, '4 + 5j' is assigned to the variable 'num_complex' to create a complex number variable.

'type()' function is used to get the type of the variable 'num_complex'.

Please have a closer look at the following example:

Example of Complex Number:

Output of the above example

    <class 'complex'>
                                

Uses of Complex Numbers in Python

Complex numbers are extensively used in mathematics, physics, engineering, and other scientific fields.

Basic operations:

Various basic operations such as addition, subtraction, multiplication, and division can be performed with float in Python.

You need to use '+' operator for addition, '-' operator for subtraction, '*' operator multiplication, and '/' operator division.

In the following example, x = 2 + 3j and y = 4 - 5j complex variables are created. Different basic operations are performed using these variables.

Please observe the following examples:

Example of basic operations of complex numbers:

Output of the above example

    (6-2j)
    (-2+8j)
    (23+2j)
    (-0.17073170731707318+0.5365853658536587j)
                                
Magnitude and Conjugate:

In the following example, the magnitude (absolute value) and conjugate (change sign of complex part) of the complex number x = 2 + 3j have been determined.

Example of magnitude (absolute value) and conjugate of complex number:

Output of the above example

    
    Magnitude of x: 3.605551275463989
    Conjugate of x: (2-3j)
                                

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python complex number variables and perform more basic and engineering calculations in the following Python code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output