VOOZH about

URL: https://www.codecademy.com/resources/docs/python/built-in-functions/complex

⇱ Python | Built-in Functions | complex() | Codecademy


Skip to Content

Python complex()

Preview: Anonymous contributor 2 total contributions
Anonymous contributor

2 total contributions

Published Jul 6, 2021Updated Aug 7, 2023

The complex() function converts a given string, or pair of numbers, into a complex number.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 CoursesIncludes 27 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      95 hours95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With CertificateWith Certificate
    • Beginner Friendly.
      24 hours24 hours

Syntax

result1 = complex(real, imaginary)
result2 = complex(string)
  • real is the real part of a complex number.
  • imaginary is the imaginary part of a complex number.
  • string is the string representation of a complex number.

Example

Use complex() to convert the string "1+7j" into a complex number:

num =complex("1+7j")
print(num)
Copy to clipboard
Copy to clipboard

This will result in the following output:

(1+7j)
Copy to clipboard
Copy to clipboard

Note: The passed string cannot contain whitespaces. A string such as, "1 + 7j", will result in an error.

Codebyte Example

Use complex() to convert the string "1.5" which is passed as a single parameter, into a complex number:

Visit us
Visit us
Hide code
Code
Output
Hide output
Copy to your clipboard

All contributors

Learn Python on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 CoursesIncludes 27 Courses
    • With Professional CertificationWith Professional Certification
    • Beginner Friendly.
      95 hours95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With CertificateWith Certificate
    • Beginner Friendly.
      24 hours24 hours