Complex() in Python | Python complex() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The complex in python programming is a method we can use to convert a string or a number into a complex data type. The complex() method takes two parameters, namely the real and imaginary values. The complex in python uses the provided parameters and returns a complex number.

Syntax of complex() in Python

The syntax of the complex in python is straightforward, it takes two parameters(the real value and the imaginary value)

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Parameters of complex() in Python

The complex in python takes two parameters. The two parameters are: real and imaginary.

real

The first parameter, i.e., the real part, is a required parameter. The first parameter's default value, i.e., real part, is set to 0.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

imaginary

The second parameter, i.e., the imaginary part, is optional. The second parameter's default value, i.e., imaginary part, is set to 0.

Note:

  • The real part or the first parameter in the complex method can be a string. If the string in the parameter is a complex number (like 10 + 7j), then we should not provide the second parameter, i.e., the imaginary part.
  • A complex number can be written as a real number multiplied by the imaginary unit i.

Return Values of complex() in Python

The complex in python generates a complex number using the parameters passed and returns the generated complex number.

Exceptions of complex() in Python

We can provide two parameters in the complex method in python. If the parameter provided is a string data type, it must be a valid complex number (like 2 + 3j). Otherwise, the complex method returns (or raises) an exception called ValueError. The exception raised will be: ValueError: complex() arg is a malformed string.

The ValueError is a built-in exception raised by python methods or functions when a function receives a correct argument type but with an appropriate value.

Example of complex() in Python

Let us take some examples to understand python's complex() method.

  1. When both the parameters are provided to the complex() method:

Output:

  1. When only the first parameter is provided to the complex() method:

Output:

What is Complex() function in Python?

The complex data type in python consists of two values, the first one is the real part of the complex number, and the second one is the imaginary part of the complex number. We usually denote the real part using i and the imaginary part with j. For example, (3 + 7j) or (3i + 7j).

To convert a string or a number into a complex number data type in python, we can use a method called complex() in python.

The complex() method takes two parameters as input: the real part of the complex number and the imaginary part of the complex number. The first parameter is necessary. On the other hand, the second parameter is optional. The complex() method returns the complex data type in python generated using the parameters provided.

We can provide a string, a number, or even a complex number as a parameter to the complex() method. If a string is provided in the parameter, it should be a valid complex number (like 10 + 7j). Otherwise, the complex method returns (or raises) a built-in exception called ValueError.

Note:

The complex() method is not the only way of creating complex data types in python. Check the More Examples section below for an explanation.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

More Examples

So far, we have learned about the complex() method of python. Now let us take some examples and understand the method better.

Convert a String into a Complex Number

When the provided parameter is a string data type, it must be a valid complex number (like 2 + 3j). Otherwise, the complex method returns (or raises) an exception called ValueError.

  1. If the string provided in the parameter is a valid complex number.

Output:

  1. If the string provided in the parameter is an invalid complex number.

Output:

Parameter Having an Integer Value

We can also provide the real number part of the complex number as a string. For example, in the complex number 3 + 0j, we can pass the real number 3 as a string parameter (like '3').

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Working With Float Type Values

We can also provide the real number part of the complex number as a floating-point number. For example, in the complex number 5.3 + 0j, we can pass the real number 5.3 as a string parameter (like '5.3').

Output:

Working With Complex Numbers as a Parameter

We can even provide a complex number as a parameter in the complex() method. For example, we can pass 2+3j as a parameter.

Output:

Creating Complex Number Without Using complex()

The complex in python is not the only way to create complex numbers. We can put I and j after the real and imaginary parts, respectively to generate the real number.

Output:

Conclusion

  • The complex in python is a method we can use to convert a string or a number into a complex number.
  • The syntax of the complex in python is: complex(real, imaginary).
  • The complex in python, takes two parameters, the first parameter is required , but the second is optional.
  • The complex in python generates a complex number using the parameters passed and returns the generated complex number.
  • If a string is provided in the parameter, it should be a valid complex number; otherwise, the ValueError exception is raised.

1. clear in python

2. rfind in python

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more