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 November 29, 2023 by Ankit Kochar

In Python, complex data types provide a versatile and powerful way to represent complex numbers. A complex number comprises a real part and an imaginary part, denoted as a + bi, where a is the real part, b is the imaginary part, and i is the imaginary unit. Python supports complex numbers as a built-in data type, allowing for arithmetic operations, mathematical functions, and various applications in scientific and engineering domains. This introduction aims to explore the fundamentals of complex data types in Python, highlighting their syntax, operations, and practical use cases.

What is Complex Data Type in Python?

The Complex Data Type in Python serves as the designated type for expressing complex numbers. As previously noted, a complex number is formatted as "a + bi." In Python, the imaginary part of a complex number is indicated using the suffix ‘j’ or ‘J’ instead of ‘i.’ For instance, 3 + 3j illustrates a complex number in Python.

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 conclusion, Python’s support for complex data types adds a layer of sophistication to numerical computations, simulations, and scientific programming. By providing a straightforward representation of complex numbers and enabling a wide range of operations, Python empowers developers to work seamlessly with mathematical models and algorithms. Whether engaged in scientific research, engineering simulations, or other computational tasks, the inclusion of complex data types enhances the language’s capabilities and versatility.

Frequently Asked Questions (FAQs) related to Complex Data Type in Python

Here are some of the FAQs related to Complex Data Type in Python:

Q1: How do I represent a complex number in Python?
A1:
In Python, you can represent a complex number using the syntax a + bj, where a is the real part, b is the imaginary part, and j is the imaginary unit.

Q2: What operations can I perform with complex numbers in Python?
A2:
Python supports various operations on complex numbers, including addition, subtraction, multiplication, division, and more. Additionally, mathematical functions such as abs(), phase(), and conjugate() are available.

Q3: Can I mix complex numbers with other numerical types in Python?
A3:
Yes, Python allows you to perform operations between complex numbers and other numerical types (integers, floats). The result will be a complex number if any operand is complex.

Q4: How do I access the real and imaginary parts of a complex number in Python?
A4:
You can access the real and imaginary parts of a complex number z using z.real and z.imag respectively.

Q5: In what scenarios are complex data types particularly useful in Python?
A5:
Complex data types in Python are particularly useful in scientific computing, engineering simulations, signal processing, and any domain involving mathematical models that include complex numbers. They provide an efficient and convenient way to handle complex arithmetic and calculations.

Leave a Reply

Your email address will not be published. Required fields are marked *