0

how to get/print value to imaginary part J/j in python3-complex numbers?

I tried using print statement as below-

x=2345+4.567J print(J) print(x.J) 

but getting below error in python 3.7-

Traceback (most recent call last): File "", line 1, in print(J) NameError: name 'J' is not defined

Traceback (most recent call last): File "", line 1, in print(x.J) AttributeError: 'complex' object has no attribute 'J'

>>> x=2345+4.567J >>> print(J) Traceback (most recent call last): File "<pyshell#132>", line 1, in <module> print(J) NameError: name 'J' is not defined >>> print(x.J) Traceback (most recent call last): File "<pyshell#134>", line 1, in <module> print(x.J) AttributeError: 'complex' object has no attribute 'J' >>> 

2 Answers 2

1

You could create another complex number with x.imag as the imaginary part and 0 as the real part:

>>> x=2345+4.567J >>> print(complex(0, x.imag)) 4.567j 

Or, use string formatting if it's just for display:

>>> x=2345+4.567J >>> print('{}j'.format(x.imag)) 4.567j 
Sign up to request clarification or add additional context in comments.

5 Comments

sir i need to get/print vale of j/J (which is assumed square-root of -1 ) not imaginary part value. suggest for this....
@Puneet j is the square-root of -1, that's it, it has no real value with decimals like real numbers do, it's just defined such that: j² = -1. Check this and this for more info on complex numbers.
I know that square of j = -1 in complex numbers but my requirement is to print value of J/j on python3.7 shell prompt(>>>). How can we do it??
@Puneet I don't understand what you mean by print the value of j/J? What do you think its value is? What is the expected output?
j/J pre-defined value in python system is square-root of -1.My expected output should be its value only. How to print value of j/J in case of complex numbers in python3.
0

You should use the "imag" attribute

>>> print(x.imag) 4.567 

1 Comment

>>> print (x.imag) will print imaginary part value not value of j/J 4.567

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.