Complex() in Python | Python complex() Function - Scaler Topics (2024)

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)

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.

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.

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:

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

Complex() in Python | Python complex() Function - Scaler Topics (2024)

FAQs

What does complex () do in Python? ›

Python complex() Function

The complex() function returns a complex number by specifying a real number and an imaginary number.

What is the use of complex function in Python? ›

Complex variable functions or complex functions are functions that assign complex numbers for complex numbers. Let C be the set of complex numbers. A function f : C → C is a rule which associates with z ∈ C, a unique w ∈ C, written as w = f(z). Thus, u and v are functions of x and y.

How to do complex calculations in Python? ›

Complex Numbers in Python
  1. complex([real[, imag]])
  2. z = complex(5, 7) print("Output:", z) # Output: (5+7j) z = complex(3) print("Output:", z) # Output: (3+0j) z = complex() print("Output:", z) # Output: 0j z = complex('1+1j') print("Output:", z) # Output: 1+1j z = complex(1, -4) print("Output:", z) # Output: 1-4j.
Feb 18, 2022

How to write complex Python code? ›

How do you write complex in Python? A complex number is of the format “a+bi.” Where a is the real number and b is the imaginary number. The complex() converts a,b into the complex integers. The real parts of the complex number can be accessed using the real function, and the imaginary part is accessed by the imag().

How do complex functions work? ›

A complex function is a function from complex numbers to complex numbers. In other words, it is a function that has a (not necessarily proper) subset of the complex numbers as a domain and the complex numbers as a codomain.

Why do we use complex functions? ›

Complex numbers are useful in physics, as well in the mathematics of real numbers, because they open a new dimension that allows to arrive at the results in a much faster and elegant way.

What is an example of a complex function in Python? ›

Python complex() Function. Python complex() function returns a complex number ( real + imaginary) example (5+2j) when real and imaginary parts are passed, or it also converts a string to a complex number.

What is the output of the function complex ()? ›

What is the output of the function complex()? Explanation: The complex function returns 0j if both of the arguments are omitted, that is, if the function is in the form of complex() or complex(0), then the output will be 0j.

What are complex words in Python? ›

Complex words here means a word which has more than 2 syllables, excluding some exceptions like words ending with "es","ed" by not counting them as a syllable. Thanks. python. string. text.

Can Python do complex math? ›

Since complex is a native data type in Python, you can plug complex numbers into arithmetic expressions and call many of the built-in functions on them. More advanced functions for complex numbers are defined in the cmath module, which is part of the standard library.

How do you input complex numbers? ›

Inputting Complex Numbers

You can use either rectangular coordinates (a+bi) or polar coordinates (r∠θ) to input complex numbers. * You can also use the following operation to input i: – [Complex] > [i].

How to convert complex to int in Python? ›

Python Complex to Int
  1. Thus, we will first use the real attribute to change the complex number to a float and then further use the int() function to convert the float data type to an integer. ...
  2. To convert a complex number into an integer, we can use the same method of explicit type conversion.
Jan 19, 2024

Is Python is a complex language? ›

Python is stable, flexible, and uncomplicated. As such, it's the ideal programming language for AI and ML applications, which involve sophisticated and often very complex algorithms. Python allows AI and ML experts to write code that's reliable, readable, and enables fast prototyping.

What is list() in Python? ›

The list() function creates a list object. A list object is a collection which is ordered and changeable.

How to implement do while in Python? ›

Python doesn't have a built-in do-while loop, but you can create one using a while loop with a condition at the end. Here's a simple example: while True: # do something if not condition: break # Output: # Depends on the condition and the task performed inside the loop.

Why do we need complex data type in Python? ›

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.

Is complex a type in Python? ›

Numeric Types — int , float , complex. There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers. Integers have unlimited precision.

Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated:

Views: 6116

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.