Python complex() Method with Examples

In the previous article, we have discussed Python bin() Method with Examples
complex() Method in Python:

By specifying a real number and an imaginary number, the complex() function returns a complex number.

Syntax:

complex(real, imaginary)

Parameters

real: This is Required. A number that represents the complex number’s real part. 0 is the default. The real number can also be a String, such as ‘2+6j’; in this case, the second parameter should be omitted.

imaginary: This is Optional. A number that represents the complex number’s imaginary part. 0 is the default.

Return Value:

The complex() method, as the name implies, returns a complex number.

The ValueError exception is thrown if the string passed to this method is not a valid complex number.

Note: Complex() always expects a string of the form real+imaginaryj or real+imaginaryJ.

Examples:

Example1:

Input:

real number = 2
imaginary number = 4

Output:

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)

Example2:

Input:

real number = 3

Output:

The complex number for the given real part{ 3 }=  (3+0j)

complex() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the number(real part) as static input and store it in a variable.
  • Give the other number (imaginary part) as static input and store it in another variable.
  • Pass the given two numbers (real & imaginary parts) as the arguments to the complex() function to get the complex number for the given real and imaginary numbers.
  • Store it in another variable.
  • Print the complex number for the given real and imaginary numbers.
  • The Exit of the program.

Below is the implementation:

# Give the number(real part) as static input and store it in a variable.
real_numbr = 2
# Give the other number (imaginary part) as static input and store it in
# another variable.
imaginry_numbr = 4
# Pass the given two numbers (real & imaginary parts) as the arguments to the
# complex() function to get the complex number for the given real and
# imaginary numbers.
# Store it in another variable.
rslt = complex(real_numbr, imaginry_numbr)
# Print the complex number for the given real and imaginary numbers.
print(
    "The complex number for the given real and imaginary numbers{", real_numbr, ",", imaginry_numbr, "}= ", rslt)

Output:

The complex number for the given real and imaginary numbers{ 2 , 4 }=  (2+4j)

Similarly, check for the other values

without giving imaginary part

real_numbr = 3
rslt = complex(real_numbr)
print(
    "The complex number for the given real part{", real_numbr, "}= ", rslt)

Output:

The complex number for the given real part{ 3 }=  (3+0j)

without giving any parameters

rslt = complex()
print("The complex number without giving any parameters = ", rslt)

Output:

The complex number without giving any parameters =  0j

When a number is given as a string?

gvn_numbr = "7+8j"
rslt = complex(gvn_numbr)
print(
    "The complex number for the given number {", gvn_numbr, "} = ", rslt)

Output:

The complex number for the given number { 7+8j } =  (7+8j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the number(real part) as user input using the int(input()) function and store it in a variable.
  • Give the other number (imaginary part) as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers (real & imaginary parts) as the arguments to the complex() function to get the complex number for the given real and imaginary numbers.
  • Store it in another variable.
  • Print the complex number for the given real and imaginary numbers.
  • The Exit of the program.

Below is the implementation:

# Give the number(real part) as user input using the int(input()) function and store it in a variable.
real_numbr = int(input("Enter some random number = "))
# Give the other number (imaginary part) as user input using the int(input()) function 
# and store it in another variable.
imaginry_numbr = int(input("Enter some random number = "))
# Pass the given two numbers (real & imaginary parts) as the arguments to the
# complex() function to get the complex number for the given real and
# imaginary numbers.
# Store it in another variable.
rslt = complex(real_numbr, imaginry_numbr)
# Print the complex number for the given real and imaginary numbers.
print(
    "The complex number for the given real and imaginary numbers{", real_numbr, ",", imaginry_numbr, "}= ", rslt)

Output:

Enter some random number = 5
Enter some random number = 6
The complex number for the given real and imaginary numbers{ 5 , 6 }= (5+6j)

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.