ScanSkill

complex

complex() returns an expression or object converted into a complex number.

Syntax

CASE I

complex(real, imaginary)

CASE II

complex(string)

Here, in CASE I:

  • real: Required. Must be a numeric type. Default is 0.
  • imaginary: Optional. Must be a numeric type. Default is 0.

In CASE II:

  • string: Optional. It must represent a valid Python numeric type if the string is used.

Note: The string must not contain whitespace around the central + or - operator while converting from a string. For eg.

complex(’2+5j’) is correct, while complex(’2 + 5j’) raises *ValueError.*

Also if the first parameter(real part) is a string, it will be represented as a complex number, and the second parameter must be omitted.

Examples

  • CASE I — int
>>> complex()
0j
>>> complex(1)
(1+0j)
>>> complex(1, 2)
(1+2j)
  • CASE I — float and complex
>>> complex(1.12, 2.34)
(1.12+2.34j)
>>> complex(1, 2.34)
(1+2.34j)
>>> complex(1+2j)
(1+2j)
>>> complex(1+2j, 3+4j)
(-3+5j)
  • CASE II — string
>>> complex('1')
(1+0j)
>>> complex('1+5j')
(1+5j)