complex numbers in python

Practical Guide to Working with Complex Numbers in Python

The Python programming language has a native complex type that allows you to work with complex numbers directly. In this guide, we’ll look at –

  • How to create complex numbers in Python?
  • Performing arithmetic operations on complex numbers in Python
  • Extract real and imaginary parts of a complex number
  • Get the conjugate of a complex number
  • Get the magnitude and phase of a complex number
  • Check if a number is real or complex in Python

How to create complex numbers in Python?

You can use the Python built-in complex() constructor to create complex numbers in Python. Pass the real and imaginary parts of the complex number as arguments to the complex() function.

The following is the syntax –

# create complex number
complex(real, imag)

If you do not specify the value for the imag parameter, it defaults to zero (that is, it will create the complex number with the imaginary part as zero.

Let’s look at an example.

# create complex number 3+4j
z = complex(3, 4)
print(z)

Output:

(3+4j)

Here, we used the complex() constructor to create the complex number (3+4j), we passed 3 as the real component and 4 as the imaginary component.

Let’s check the type of the returned value.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

type(z)

Output:

complex

You can see that the returned value is of complex type.

Note that you can also directly create a complex number in Python using a complex literal – a + bj where a and b are numeric constants.

# create complex number
z = 3+4j
type(z)

Output:

complex

We get the resulting complex number. While this method works, we recommend using the complex() constructor to create complex numbers in Python as it is more flexible – you can pass variable values (or values resulting from expressions) to the real and imaginary parts.

Performing arithmetic operations on complex numbers in Python

You can use the common arithmetic operators (+, -, *, and /) directly to perform arithmetic operations on complex numbers in Python.

Addition

Use the + operator to add two (or more) complex numbers together.

# create two complex numbers
z1 = complex(2, 5) # 2+5j 
z2 = complex(1, 2) # 1+2j
# add complex numbers
z = z1 + z2
print(z)

Output:

(3+7j)

Here, we add two complex numbers together. You can see that the real and imaginary parts of the resulting complex number are the sum of the corresponding real and imaginary parts of the individual complex numbers in the addition operation.

Subtraction

Use the - operator to subtract two complex numbers.

# create two complex numbers
z1 = complex(2, 5) # 2+5j 
z2 = complex(1, 2) # 1+2j
# subtract z2 from z1
z = z1 - z2
print(z)

Output:

(1+3j)

Here, we subtract two complex numbers. You can see that the respective real and imaginary coefficients are subtracted. Note that the subtraction operation in mathematics is not commutative, that is, z1 - z2 != z2 - z1.

Multiplication

Use the * operator to multiply two (or more) complex numbers together. Note that, when you multiply complex numbers a+bj and c+dj the resulting complex number is (ac-bd)+(ad+bc)j.

# create two complex numbers
z1 = complex(2, 5) # 2+5j 
z2 = complex(1, 2) # 1+2j
# multiply z1 and z2
z = z1 * z2
print(z)

Output:

(-8+9j)

The resulting complex number is the result of the multiplication of complex numbers z1 and z2.

Division

The image below illustrates how the division of two complex numbers is calculated in mathematics.

division of two complex numbers

Use the / operator to divide two complex numbers in Python.

# create two complex numbers
z1 = complex(2, 5) # 2+5j 
z2 = complex(1, 2) # 1+2j
# divide z1 by z2
z = z1 / z2
print(z)

Output:

(2.4+0.2j)

Here, we divide z1 by z2. Note that the division operation in mathematics is not commutative, that is, z1 / z2 != z2 / z1.

Extract real and imaginary parts of a complex number

The numpy module in Python comes with built-in functions to directly extract the real and imaginary parts of a complex number.

Use the numpy.real() function to get the real part of a complex number. You can pass a complex number or an array of complex numbers as an argument.

Let’s look at an example.

import numpy as np

# create complex number
z = complex(3, 4)
# get the real part
print(np.real(z))

Output:

3.0

We get the real component of the complex number as the output.

Use the numpy.imag() function to get the imaginary part of a complex number. You can pass a complex number or an array of complex numbers as an argument.

Let’s look at an example.

import numpy as np

# create complex number
z = complex(3, 4)
# get the imaginary part
print(np.imag(z))

Output:

4.0

We get the imaginary component of the complex number as the output.

Get the conjugate of a complex number

The complex conjugate of a complex number is obtained by changing the sign of the imaginary part. For example for the complex number a + bj, the complex conjugate would be a - bj.

Use the numpy.conj() function to get the conjugate of a complex number. You can pass a complex number or an array of complex numbers as an argument.

Let’s look at an example.

import numpy as np

# create complex number
z = complex(3, 4)
# get the conjugate
z_bar = np.conj(z)
# display the complex number
print(z)
# display the conjugate
print(z_bar)

Output:

(3+4j)
(3-4j)

We get the conjugate of the complex number z.

Get the magnitude and phase of a complex number

The magnitude (or the absolute value) of a complex number is defined as its euclidean norm. The phase (or angle) of a complex number is defined as the angle the complex number vector makes with the real axis in the counter-clockwise direction.

The below image shows the magnitude as well as the phase angle of a complex number.

graph showing the magnitude and phase of a complex number

Use the Python built-in abs() function to get the absolute value (or magnitude) of a complex number.

import numpy as np

# create complex number
z = complex(3, 4)
# get the absolute value
print(abs(z))

Output:

5.0

We get the absolute value of the complex number 3+4j as 5.

Use the numpy.angle() function to get the phase angle of a complex number. You can pass a complex number or an array of complex numbers as an argument.

It returns the phase angle in radians by default. To get the phase angle in degrees, pass deg=True.

import numpy as np

# create complex number
z = complex(3, 4)
# get the phase angle in radians
print(np.angle(z))
# get the phase angle in degrees
print(np.angle(z, deg=True))

Output:

0.9272952180016122
53.13010235415598

Here, we get the phase angle for the complex number 3+4j in radians and degrees using the numpy.angle() function.

Check if a number is real or complex in Python

Use the numpy.isreal() function to check if a number is a real number or not (it checks whether the imaginary part is zero or not). You can pass a scaler value or an array of values.

Let’s look at an example.

import numpy as np

# check if number is real or not
print(np.isreal(12))
print(np.isreal(12+3j))
print(np.isreal(3j))

Output:

True
False
False

We get True only for the real argument.

Use the numpy.iscomplex() function to check if a number is a complex number or not (it checks whether it has a non-zero imaginary part). You can pass a scaler value or an array of values.

Let’s look at an example.

import numpy as np

# check if number is complex or not
print(np.iscomplex(12))
print(np.iscomplex(12+3j))
print(np.iscomplex(3j))

Output:

False
True
True

We got True for all the values with a non-zero imaginary part.

Summary

In this tutorial, we looked at how to create complex numbers in Python and perform common operations on them with the help of examples and illustrations. The following are the key takeaways from this tutorial –

  • Use the complex() construct to create complex numbers in Python.
  • You can directly use the arithmetic operators (+, -, *, /) to perform arithmetic operations on complex numbers.
  • Use the numpy.real() function to extract the real part of the complex number and the numpy.imag() function to extract the imaginary part.
  • To get the complex number conjugate, use the numpy.conj() function.
  • For a complex number, use the abs() built-in function to get its magnitude (absolute value) and use the numpy.angle() function to get its phase angle.
  • To check if a number is real, use the numpy.isreal() function and to check if a number is complex, use the numpy.iscomplex() function.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top