﻿ Complex Numbers in Python ﻿ # Complex Numbers in Python In this article, we explain complex numbers and how to code them in Python.

So, just to the basic definition or composition of a complex number, complex numbers are numbers that contain a real and imaginary part.

Thus, the number, 3 +4j, is a complex number.

The 3 is the real part of the number. And the 4 is the imaginary part of a number.

Complex numbers are used for many different things, including representation of impedance of reactive electronic components, such as capacitors and inductors. In this case, the real part of the number represents the magnitude of the impedance and the imagninary part represents the phase.

So complex numbers represent different things, depending on its application.

Python has built-in functionality to deal with complex numbers.

In fact, you can just create a variable and initialize it to a complex number in Python, without having to import any type of modules.

The code below does this.

So the code above is very basic.

We create a variable, c1, and set it equal to, 3 + 7j

Now if you check the type of the variable, c1, you will see that the class 'complex' is returned.

So you see that the number is a complex number.

In Python, j is the letter used to represent the imaginary part of a complex number. Even though some mathematics guides use i instead of j in the imaginary part, this will give an error in Python. Only j can be used for the imaginary part.

And this is how we can create a complex number in Python.

### How to Obtain the Real and Imaginary Parts of the Complex Number

Let's say you've created a complex number and you want to obtain either the real and/or the imaginary parts of the complex number.

Python has a way for this to be done.

We can get the real part of a complex number by the real attribute and get the imaginary part of a complex number by the imag attribute.

This is shown in the code below.

So we create a complex number and store it in the c1 variable.

We then take this complex number and get the real part of it with the real attribute. We then get the imaginary part of this complex number with the imag attribute.

As you can see, the real and imag attributes return the value as a floating point.

### How to Obtain the Conjugate of a Complex Number

Next, we show how to obtain the conjugate of a complex number in Python.

The conjugate of a complex number is the complex number with the same exact real part but an imaginary part with equal but opposite magnitude.

So, for example, the conjugate for 3 + 4j would be 3 -4j.

The conjugate for a complex number can be obtained using the conjugate() function.

This is shown in the code below.

So we create a complex number and store it in the c1 variable.

We then get the conjugate of this complex number using the conjugate() function in Python.

So you can see that the conjugate of 3 + 7j is 3-7j.

### How to Obtain the Magnitude of a Complex Number

Now we will show how to obtain the magnitude of a complex number in Python.

Doing so is extremely simple.

All you have to do is use the abs() function to get the magnitude a complex number.

This is shown in the code below.

So you can see that we have the complex number, 3+4j

Using the abs() function, we get the output, 5.0, which is the magnitude of the complex number.

And this is the majority of the functions that you will have to deal with regarding complex numbers in Python.

Related Resources

How to Randomly Select From or Shuffle a List in Python