  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Complex Data Type in Python

Last Updated on March 13, 2023 by Prepbytes Complex numbers are employed in a wide range of applications. They are used in almost every domain, from Higher Mathematics to Electronics and Electrical Engineering. A complex number is of the form “a + bi”, where a is the real part, ‘b’ is the imaginary part, and ‘i’ is defined as the square root of “-1”.

## What is Complex Data Type in Python?

The Complex Data Type in Python is the data type that is used for representing complex numbers. As mentioned above a complex number is represented in the form of “a + bi’. In Python, we use the j or J suffix instead of i, for denoting the imaginary part of the complex number. For example, 3 + 3j.

### Properties of Complex Data Type in Python

The Complex Data Type in Python has the following properties:

• Real Part: In a complex number, a + bi, ‘a’ represents the real part of the number. For accessing the real part of the complex numbers, we can use real() method in Python.
• Imaginary Part: For a complex number a + bi, ‘b’ represents the imaginary part. We can use imag() method for accessing the imaginary part of complex numbers in Python.
• Conjugate: The conjugate of a complex number is de defined as the number obtained by flipping the sign of the imaginary part. In Python, we use conjugate() for computing the conjugate of the complex number.
• Magnitude: The magnitude of a complex number is defined as the distance of the point from the origin in the complex plane or argand plane. We use abs() for calculating the Magnitude of a Complex Number in Python.
• Phase: The angle between the vector representing the complex number in Argand Plane and the positive real axis is defined as the Phase of the Complex Number. The phase() method is used for computing the phase of a complex number in Python.

### Using Complex Data Type in Python

For using and creating a variable of Complex Data Type in Python, we use the complex() method. This method can be used for converting a number or string into a complex Data Type in Python.

### Syntax of complex() in Python

The syntax of the complex() Function is pretty much simple as specified below:

``complex(real, imaginary)``

### Parameters of complex() in Python

The complex() Function in Python can take two Parameters, namely:

• real: The first parameter of the complex() Function defines the real part of the complex number. It is an optional parameter and its default value is 0 is we don’t specify it.
• imaginary: The second parameter defines the imaginary part of the complex number. It is also an optional Parameter and it takes a default value of 0 if we do not specify it.

### Return Type of complex() in Python

This function returns a Complex Number that is being generated with the help of the arguments passed to the function.

### Examples of Complex Data Type in Python

Let us discuss some examples for a deeper understanding of the concept.

Example 1 of Complex Data Type in Python
This example will demonstrate how to use the complex() method for defining Complex Data Type in Python.

Code:

```real = 3
imaginary = 5

z = complex(real, imaginary)
print("The complex number is ", z)```

Output:

``The complex number is  (3+5j)``

Explanation:
In this code, we have first declared two variables namely real and imaginary. Then we declare a variable of complex data type in python using the complex() function and pass the real and imaginary variables as parameters to the function. At last, we printed the generated complex number on the console.

Example 2 of Complex Data Type in Python
We can also declare a Complex Data Type in Python using the string literal of type “a + bj”.

Code:

```# Defining Complex Number using the string literal
complex_num = 2 + 4j

print("The complex number is ", complex_num)```

Output:

``The complex number is  (2+4j)``

Explanation:
In the above code, we have declared a variable complex_num as a complex Number using the String Literal with real part as 2 and imaginary part as 4. Then we printed the number on the output console.

Example 3 of Complex Data Type in Python
The Arithmetic Operations such as Addition, Subtraction, Multiplication, and Division can be performed on the Complex Data Type in Python.

Code:

```# Declaring Complex Numbers
a = 2 + 3j
b = 1 - 2j

# Performing Arithmetic Operations
z1 = a + b
z2 = a - b
z3 = a * b
z4 = a / b

# Printing Output
print("The result of addition is ", z1)
print("The result of subtraction is ", z2)
print("The result of multiplication is ", z3)
print("The result of division is ", z4)```

Output:

``````The result of addition is  (3+1j)
The result of subtraction is  (1+5j)
The result of multiplication is  (8-1j)
The result of division is  (-0.8+1.4j)``````

Explanation:
In the above-coded example, we have declared two complex numbers and then performed the basic arithmetic operations using the operators and then printed the resulting Complex numbers on the output screen.

Example 4 of Complex Data Type in Python
Let us see how to calculate the Conjugate of a complex Data Type in Python using the predefined function named “conjugate()”.

Code:

```# Declaring Complex Number
a = 2 + 3j

# Computing conjugate
a_conj = a.conjugate()
print("The conjugate of ", a, " is ", a_conj)```

Output:

``The conjugate of  (2+3j)  is  (2-3j)``

Explanation:
The above code shows how we can calculate the conjugate of a number. First, we declared the number and then we computed the conjugate using the conjugate() function as you can see in the output on the console screen.

Example 5 of Complex Data Type in Python
We can use real() and imag() functions for directly accessing the real and imaginary parts of the complex data type in python.

Code:

```# Complex Number
z = 2 + 5j

# Accessing the real and imaginary part
real_part = z.real
imag_part = z.imag

print("The real part is ", real_part)
print("The imaginary part is ", imag_part)```

Output:

``````The real part is  2.0
The imaginary part is  3.0``````

Explanation:
In this example, we have declared a complex variable and then printed the real and imaginary parts which we have accessed using the real and imag Functions.

Conclusion
In this article, we have learned about the Complex Data Type in Python, and how to declare it using the complex() keyword. We have seen many examples demonstrating the usage of arithmetic operations on complex numbers, accessing real and imaginary parts by using the ‘real’ and ‘imag’ functions. This complex Data Type in Python is used in various mathematics, scientific and engineering applications.

Ques 1. How do you convert a string into a Complex Data Type in Python?
Ans. We can convert a string into Complex Number by using the complex() Function. For example,
complex_num = complex("2 + 3j")

Ques 2. What different types of Operations can we perform on the Complex Data Type in Python?
Ans. We can perform the Arithmetic Operations as well as logical Operations on Complex Data Type in Python.

Ques 3. Can we perform Operations between a Complex Number and a Real Number in Python?
Ans. Yes, we can perform Arithmetic operations (such as +, -, *, /) between Complex and Real Numbers in Python.

Ques 4. State the difference between Float and Complex Data Type in Python.
Ans. A complex data type in Python represents a number that consists of a real part and an imaginary part, while a float data type represents a real number with a decimal point.

Ques 5. Can we use math Module with Complex Data Type in Python?
Ans. No, we cannot use the math module with the Complex Data Type in Python.