Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Complex () function in Python?
3.
Syntax of complex() in Python
4.
Parameters of complex() in Python
5.
Return Values of complex() in Python
6.
Implementation 
6.1.
Integer Type
6.2.
Python
6.3.
Output
6.4.
Float Type 
6.5.
Python
6.6.
Output
7.
Exceptions of complex() in Python
8.
Example of complex() in Python
8.1.
Python
8.2.
Python
8.3.
Python
8.4.
Python
8.5.
Python
9.
Frequently Asked Questions
9.1.
How do you find complex in Python?
9.2.
Why is complex data type used in Python?
9.3.
What is an example of a complex in Python?
9.4.
What do you mean by Python built-in function?
10.
Conclusion
Last Updated: Mar 27, 2024
Easy

Complex() Function in Python

Author Kanak Rana
0 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

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 a built-in function. What is a built-in function in Python, you may ask? Python built-in functions are functions that have pre-defined functionality in Python. The Python interpreter has many functions that are always available for use. These are referred to as Built-in Functions. 

The complex() function returns a complex number in the form of real and imaginary numbers.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of complex() in Python

complex(real, imaginary)

Parameters of complex() in Python

There are two types of parameters.

Real It is a real part of a complex number. It is a default value, i.e., it is set to 0.
Imaginary It 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))

Output

output

Float Type 

  • Python

Python

p=input("Enter a float:")
complex = complex(p)
print("Complex number is: ", complex)

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)

Output

(2+3j)

 

2. Using Variables:
 

  • Python

Python

real_part = 5
imaginary_part = -2
result = complex(real_part, imaginary_part)
print(result)

Output

(5-2j)

 

3. Using Numeric Literals:

  • Python

Python

result = complex(1.5, -2.5)
print(result)

Output

(1.5-2.5j)

 

4. String Representation:

  • Python

Python

complex_str = complex("2.5+3j")
print(complex_str)

Output

(2.5+3j)

 

5. Zero Imaginary Part:

  • Python

Python

result = complex(7)
print(result)

Output

(7+0j)

Frequently Asked Questions

How do you find complex in Python?

Complex numbers in Python can be created using the complex() function, specifying the real and imaginary parts as arguments, e.g., complex(2, 3).

Why is complex data type used in Python?

The complex data type in Python is used to represent numbers with both real and imaginary parts, essential for mathematical and scientific computations involving complex arithmetic.

What is an example of a complex in Python?

An example of a complex number in Python is complex(2.5, 3), representing a number with a real part of 2.5 and an imaginary part of 3.

What do you mean by Python built-in function?

Python built-in functions are functions that have pre-defined functionality in Python. The Python interpreter has many functions that are always available for use. These are referred to as Built-in Functions.

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? 

Previous article
OS Path Splitext() Method
Next article
numpy.gradient() Method in Numpy
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass