-3

I want to assign an imaginary number to a variable:

import math

a = sqrt(4)j
print(a)

This results in a SyntaxError:

Line 3: SyntaxError: bad input ('j')

I can assign an imaginary number to variable like this:

a = 2j

How do I solve this?

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
FDuldul
  • 167
  • 1
  • 10

2 Answers2

9

You can simply use complex() to return:

real + imag*1j or convert a string or number to a complex number

>>> a = complex(0,math.sqrt(4))
>>> a
2j
user3483203
  • 50,081
  • 9
  • 65
  • 94
3

Use the complex function.

>>> complex(0, sqrt(4))
2j
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149