-4

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
  • 34,416
  • 14
  • 83
  • 119
FDuldul
  • 157
  • 9

2 Answers2

8

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
  • 48,205
  • 9
  • 52
  • 84
3

Use the complex function.

>>> complex(0, sqrt(4))
2j
Aran-Fey
  • 35,525
  • 9
  • 94
  • 135