Table of contents
1.
Introduction
2.
What is Complex () function in Python?
2.1.
Syntax of complex() in Python
2.2.
Parameters of complex() in Python
2.3.
Return Values of complex() in Python
3.
Implementation 
3.1.
Integer Type
3.2.
Python
3.3.
Output
3.4.
Float Type 
3.5.
Python
3.6.
Output
4.
Exceptions of complex() in Python
5.
Example of complex() in Python
5.1.
Python
5.2.
Python
5.3.
Python
5.4.
Python
5.5.
Python
6.
Frequently Asked Questions
6.1.
What is a complex expression in Python?
6.2.
What is a complex data type in Python?
6.3.
What is a complex object in Python?
7.
Conclusion
Last Updated: Sep 21, 2024
Easy

Complex() Function in Python

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

As you may know, Python is one of the most used programming languages because its syntax is simple, direct, straightforward, and elegant, allowing code to be written in fewer lines than other programming languages. It is an object-oriented programming language and an interpreted language, making it ideal for rapid application development.

 Complex in Python


This article will discuss the complex function in Python, a mathematical function used to ease the concept of finding the complex number in mathematics.

Let's start with a basic introduction to Python and complex numbers proceeding with complex in Python.

Also see, Intersection in Python, and Convert String to List Python

What is Complex () function in Python?

In Python, the complex() function is used to create complex numbers. It takes two arguments, representing the real and imaginary parts of the complex number, and returns a complex number object. If no arguments are provided, it returns 0j. The complex() function can also parse strings in the format 'real+imagj' to create complex numbers directly from string representations.

Syntax of complex() in Python

complex(real, imaginary)

Parameters of complex() in Python

There are two types of parameters.

RealIt is a real part of a complex number. It is a default value, i.e., it is set to 0.
ImaginaryIt is an imaginary part of the complex and an optional value set to 0.

Note: A string can be used as the fundamental or real part or the first parameter in the complex method. If the parameter string is a complex number (such as 6 + 3j), we should not include the second parameter, i.e., the imaginary part.

Return Values of complex() in Python

In Python, the complex() function is used to create a complex number. It returns a complex number in the form a + bj.

Implementation 

There are several ways to show the working complex in Python. Some of them are mentioned below:

Integer Type

  • Python

Python

print("Enter the number")
z = input()
print("Complex number is")
print (complex(z))
You can also try this code with Online Python Compiler
Run Code

Output

output

Float Type 

  • Python

Python

p=input("Enter a float:")
complex = complex(p)
print("Complex number is: ", complex)
You can also try this code with Online Python Compiler
Run Code

Output

output

You can compile it with online python compiler.

Exceptions of complex() in Python

The complex() function in Python can raise exceptions in certain situations. Here are some scenarios where exceptions may occur:

1. Invalid Argument Types: If the complex() function is called with arguments that are not numeric (e.g., strings, lists, or other non-numeric types), a TypeError will be raised.

result = complex("3", "4")  # Raises TypeError

 

2. Invalid String Format for Real or Imaginary Part: If the function is called with a string argument that doesn't represent a valid numeric value, a ValueError is raised.

result = complex("abc", 4)  # Raises ValueError

 

3. Too Many Arguments: If more than two arguments are provided to the complex() function, a TypeError is raised.

result = complex(3, 4, 5)  # Raises TypeError

 

4. Invalid Imaginary Suffix: If a numeric literal is used with an invalid imaginary suffix (other than 'j' or 'J'), a SyntaxError will be raised.

result = complex(3, 4i)  # Raises SyntaxError

Example of complex() in Python

Let us understand complex() function in Python with the help of different types of examples:

1. Basic Example:

  • Python

Python

result = complex(2, 3)
print(result)
You can also try this code with Online Python Compiler
Run Code

Output

(2+3j)

 

2. Using Variables:
 

  • Python

Python

real_part = 5
imaginary_part = -2
result = complex(real_part, imaginary_part)
print(result)
You can also try this code with Online Python Compiler
Run Code

Output

(5-2j)

 

3. Using Numeric Literals:

  • Python

Python

result = complex(1.5, -2.5)
print(result)
You can also try this code with Online Python Compiler
Run Code

Output

(1.5-2.5j)

 

4. String Representation:

  • Python

Python

complex_str = complex("2.5+3j")
print(complex_str)
You can also try this code with Online Python Compiler
Run Code

Output

(2.5+3j)

 

5. Zero Imaginary Part:

  • Python

Python

result = complex(7)
print(result)
You can also try this code with Online Python Compiler
Run Code

Output

(7+0j)

Frequently Asked Questions

What is a complex expression in Python?

A complex expression in Python involves multiple operations and data types, often combining arithmetic, logical, or conditional operations in one line.

What is a complex data type in Python?

The complex data type in Python represents numbers with a real part and an imaginary part, written as real+imagj.

What is a complex object in Python?

A complex object in Python is an instance of the complex class, representing numbers with both real and imaginary components.

Conclusion

We have discussed Complex() Function in Python in this article. The complex() function in Python serves as a fundamental tool for handling complex numbers, a crucial component in mathematical and scientific computations. Whether creating complex numbers from explicit values, variables, or string representations, the versatility of the complex() function empowers developers to work seamlessly with both real and imaginary components.

We encourage you to check out our other articles to learn more about Python, like the complex in Python. 

We hope you have gained a better understanding of these topics now! Are you planning to ace the interviews with reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Live masterclass