How to Create an Array of Complex Numbers in Python

This tutorial is designed to guide you on how to create an array of complex numbers in Python. Complex numbers in Python consist of a real and an imaginary part. With the help of the NumPy library, we can instead create multi-dimensional arrays of complex numbers with ease.

Step 1: Importing Necessary Library

In order to utilize the power of arrays, we need to use the NumPy library. Don’t worry if you haven’t used or heard of NumPy before, it is basically a library in Python used for working with arrays. The first step will be importing NumPy into our Python script. This can be done with the following code:

Step 2: Define Your Complex Numbers

Next, let’s define our complex numbers. In Python, a complex number is defined using the “j” notation, not “i” as used in mathematics. For example,

Step 3: Create an Array of Complex Numbers

The final step is to create our array by using the numpy.array() function and passing our complex numbers as elements into this function. This can be done as follows:

Now, you have successfully created an array of complex numbers. You can print this array to confirm:

If everything has been followed correctly up to this point, your output should be:

[3+4j 2-5j]

Full Python Code

Here’s the full Python code that includes all the steps mentioned above:

Conclusion

Congratulations! Now you have learned how to create an array of complex numbers in Python using the NumPy library. The concept of complex numbers and multi-dimensional arrays is a must-know for every aspiring data scientist or programmer aiming to work extensively with data in Python.

You can explore the NumPy library further to find out more about various other functionalities that it offers.