# Complex Numbers in Python

We commonly need to execute calculations on numeric data types, including complex numbers, whether working on machine learning, or scientific computations. So in this tutorial, we will discuss how to write a Python Program to create a complex number in Python. And also we are going to cover the below topics.

• Complex numbers in Python NumPy
• Complex numbers in Python Scipy
• Complex numbers in Python matplotlib
• Complex numbers in Python class
• Complex numbers in Python imaginary
• Complex numbers in Python user input
• Complex number Python real part
• addition of complex numbers in Python
• Python power of a complex number
• Complex number in string Python
• Complex number division Python
• How to multiply complex numbers in Python
• How to compare complex numbers in Python
• How to round complex numbers in Python

## Complex Numbers in Python

• When both the real and imaginary parts are given, the complex() method produces a complex number; otherwise, it converts a string to a complex number.
• Any integer that can be expressed as (a+b j), where a and b are real numbers, is referred to as a complex number. In this case, the imaginary number j is defined as being equal to the square root of -1.
• The square roots of negative numbers are frequently utilized in calculations involving complex numbers, which typically occur in pairs.
• By simply declaring a variable and assigning an expression of the type (a+bj), we can define complex numbers in Python. Here, “a” and “b” should be literal numbers, while “j” can be any letter of the alphabet.

Syntax:

Here is the Syntax of the complex() function in Python

``complex([real[, imag]])``

Example:

Let’s take an example and check How to create a complex number in Python

``````complex_number = 7+5j
print('Type of complex number is',type(complex_number))``````

Without utilizing the complex() method, a complex number can be produced. To do that, you must follow a number with “j” or “J.”

Here is the Screenshot of the following given code

This is how we can create a complex number program in Python and also we have checked the datatype.

## Complex numbers in Python NumPy

• Here we will discuss how to create a complex number by using NumPy Python.
• To perform this particular task we are going to use the np.zeros() function One of the most important functions that are often utilized in machine learning programs is the numpy.zeros() function. In order to create an array of zeros, use this function.
• The numpy.zeros() function creates a new array of the specified type and form that is entirely composed of zeros.

Example:

``````import numpy as np
# Creation of complex number
Z = np.zeros(8, dtype=complex)
# Display the Result
print(Z)``````

You can refer to the below Screenshot

In this example, we have discussed how to create a complex number by using NumPy library.

## Complex numbers in Python matplotlib

• “x + yi” is used to represent a complex number. Using the complex function, Python transforms the real numbers x and y into complex numbers (x,y). The function real() can be used to retrieve the real portion, and imag can be used to represent the imaginary portion().
• You can plot complex numbers on a polar plot. If you have a list of complex numbers.

Example:

``````import matplotlib.pyplot as plt
import numpy as np

# Creating of array by assigning complex numbers
numbers = np.array([2+6j, 5+9j, 1-2j, -4+9j, 7+2j, 1+9j])

# Real part
real = numbers .real
# Imaginary part
imag = numbers .imag

# plot the complex numbers
plt.plot(real, imag, 'g*')
plt.ylabel('Imaginary')
plt.xlabel('Real')
plt.show()``````

Here is the implementation of the following given code.

This is how we can create a complex number in Python by using matplotlib.

## Complex numbers in Python imaginary

• A complex number’s imaginary portion is equal to the real number multiplied by i.
• For difficult numerical mathematics tasks, Python includes a built-in module that you can easily use the cmath module.
• This module’s methods take int, float, and complex values as inputs. It even supports Python objects with the complex() and float() methods.
• A complex number is usually always returned by the functions in this module. If the return value can be written as a real integer, the imaginary part of the return value is equal to 0. Several methods and constants are available in the cmath module.

Example:

``````import cmath
# Declaring integer numbers
m = 8
n = 7

# converting m and n into complex number by using complex() function
z = complex(m,n);
print(z)
# Displaying imaginary part
print ("The imaginary part of complex number is : ",z.imag)``````

Here is the implementation of the following given code

As you can see in the Screenshot we have discussed how to extract the imaginary part in a complex number.

## Complex numbers in Python user input

• Users can input data using Python. As a result, we can request input from the user.
• In this example, we have just used the input function. In Python, the input() and print() functions are used to receive input from users and produce output displayed on the screen.
• Users can supply the application with any information in the form of texts or numbers by using the input() function.

Example:

Let’s take an example and check how we can use the input function from the input value and get the complex number.

``````complx = complex(input("Enter the value:-"));
print(complx)``````

Here is the Screenshot of the following given code.

This is how we can create a complex number program by taking input from the user

## Complex number Python real part

• In this section, we will discuss how to extract the real part in Python complex numbers.
• To find the real part of the complex number we can easily use the concept of the real() function.
• A complex number is usually always returned by the functions in this module. If the return value can be written as a real integer, the imaginary part of the return value is equal to 0. Several methods and constants are available in the cmath module.

Example:

``````import cmath
# Declaring integer numbers
z = 4
x = 9

# converting m and n into complex number by using complex() function
y = complex(z,x);
print(y)
# Displaying imaginary part
print ("The real part of complex number is : ",y.real)``````

In this example, we have declared two variables and assigned the integer values to them, and then used the complex() function within this function we passed two input variables and it will display the complex number.

Now we want to extract the real part from the complex number for this we have used the real() function.

Here is the implementation of the following given code.

This is how we can get the real part from the complex number in Python.

## Addition of complex numbers in Python

• Complex numbers are those whose expression is written as a+bi, where a and b are real numbers and I is an imaginary number.
• To perform this particular task first we have defined the function addcomplex() and within this function, we assigned two variables in which we assigned the integer numbers.

Example:

``````def addComplex( a1, b2):
return a1 + b2

a1 = complex(8, 9)
b2 = complex(2, 5)

Now to add two complex numbers we have used the + operator and it will add two input variables.

You can refer to the below Screenshot.

This is how we can add the two input complex numbers in Python.

## Python power of a complex number

• The built-in pow() or the binary exponentiation operator (**) can both be used to raise complex numbers to power, but not the math module’s version, which only supports floating-point values.
• A number’s power indicates how many times it should be multiplied. Other names for powers include exponents and indices. For instance, 6^2 could be referred to as “6 squared,” “6 to the power 2.”
• In this example, we have declared two variables and assigned the integer values to them, and then used the complex() function within this function we passed two input variables and it will display the complex number.

Example:

``````import cmath
# Declaring integer numbers
p = 4
q = 9

# converting m and n into complex number by using complex() function
y = complex(p,q);
print(y)
# Displaying the power of a complex number
print ("power of complex number is : ",y**2)``````

You can refer to the below Screenshot.

As you can see in the Screenshot we get the power of a complex number in Python.

## Complex number in string Python

• Here we will discuss how to use the complex number in Python string format.
• To perform this task we are going to use the complex() function and to create complex numbers from numbers or strings, use the complex() function in Python. This function returns a complex number and accepts two optional parameters. The first parameter is referred to as a real component and the second is imaginary.

Example:

``````complex_number = ('7+5j')
print('Type of complex number is',type(complex_number))``````

Without utilizing the complex() method, a complex number can be produced. To do that, you must follow a number with “j”. In this example, we will use the string complex number and then use the type() function to get the datatype of an input number.

Here is the execution of the following given code.

As you can see in the Screenshot we have discussed how to use the complex number in Python string format.

## Complex number division Python

• In Python, complex numbers can be divided using the / operator and it divides the first operand by the second element.
• In this example, we have used the two variables and converted them into a complex number by using the complex() function. Next, we will divide the given complex numbers.

Example:

Let us take an example and understand how to divide complex numbers in Python.

Source Code:

``````def complex( y1, y2):
return y1 / y2

y1 = complex(3, 6)
y2 = complex(2, 3)

print( "Division of given complex number :", (complex(y1, y2)))
``````

You can refer to the below Screenshot

In this example, we understand how to divide the complex number inputs in Python.

## How to multiply complex numbers in Python

• In this section, we will discuss How to multiply complex numbers in Python.
• Here we will use the * operator to get the multiplication of two complex numbers. In Python multiplication, we multiply two numbers by using the star operator.

Example:

Let us take an example and understand how to multiply complex numbers in Python.

``````def complex( y1, y2):
return y1 * y2

y1 = complex(3, 6)
print(y1)
y2 = complex(2, 3)
print(y2)

print( "Multiplication of given complex number :", (complex(y1, y2)))``````

In this example, we have used two complex numbers by using the complex() function and then used the * operator to get the multiplication of two input numbers.

Here is the implementation of the following given code.

This is how we can multiply the complex numbers in Python by using the * operator.

## How to compare complex numbers in Python

• Let us discuss how to compare two complex numbers in Python.
• To perform this particular task first we will declare two variables and assign complex numbers and then for comparing the complex number we will use the == operator.
• The purpose of the relational operators commonly referred to as comparison operators are to return either true or false depending on the value of the operands.

Example:

``````complex_num1 = 10+3j
complex_num2 = 40+6j
print(complex_num1 == complex_num2)``````

Here is the Screenshot of the following given code

In this example, we have understood how to compare complex numbers in Python.

## How to round complex numbers in Python

• In this section, we will discuss how to round complex numbers in Python.
• To do this task we will use the round() function and Python comes with a built-in function called round(). It will offer you a float number in return, rounded to the inputted decimal points.
• If the number of decimal places to round is not supplied, it will round to the nearest integer, which is treated as 0.

Example:

Let’s take an example and check how to get the rounded number in the case of a complex number.

Source Code:

``````m = 8.45 + 7.56j
round(m.real, 1) + round(m.imag, 1) * 1j``````

Here is the implementation of the following given code

This is how we can get the round figure number of complex number datatype in Python.

You may also like to read the following Python tutorials.

In this article, we have discussed how to create a complex number program in Python in different ways and methods. Also, we have covered the following given topics.

• Complex numbers in Python NumPy
• Complex numbers in Python Scipy
• Complex numbers in Python matplotlib
• Complex numbers in Python class
• Complex numbers in Python imaginary
• Complex numbers in Python user input
• Complex number Python real part
• addition of complex numbers in Python
• Python power of a complex number
• Complex number in string Python
• Complex number division Python
• How to multiply complex numbers in Python
• How to compare complex numbers in Python
• How to round complex numbers in Python