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.

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.

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

Float Type

Python

Python

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

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:

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 Amazon, Google, Microsoft, and more?

Guided path

Free

Basics of python

7 chapters

127+ Problems

Earn badges and level up

Live masterclass

Why SQL is the vital tool to join Product management teams?

by Bhasin Yatin, Product Manager @ Amazon

19 Apr, 2024

01:30 PM

Expert guidance on how to become a Data/Business Analyst from a Non-Tech role?

by Prerita Agarwal, Data Specialist @ Microsoft

17 Apr, 2024

01:30 PM

Project ideas to get shortlisted for SDE interview at Microsoft and Paypal and more.

by Pranav Malik, SDE2@Microsoft

18 Apr, 2024

01:30 PM

Why SQL is the vital tool to join Product management teams?

by Bhasin Yatin, Product Manager @ Amazon

19 Apr, 2024

01:30 PM

Expert guidance on how to become a Data/Business Analyst from a Non-Tech role?