The n
object defines a __str__
method which is called when print
tries to convert it to a string. You can access the parts individually if you need to print them differently. For example, to print just the real, then just the imaginary part:
for i in range(100):
n = i + 1j
print(n.real, n.imag)
This output gives:
0.0 1.0
1.0 1.0
2.0 1.0
3.0 1.0
...
So the real and imaginary parts are being interpreted as float
now instead of complex
, but you still have the trailing .0
which you might not want. If you know everything is an int
, you can change the formatting:
for i in range(100):
n = i + 1j
print(int(n.real), int(n.imag))
Or, you can ask python to guess what the best format will be:
for i in range(100):
n = i + 1j
print(f"{n.real:g}", f"{n.imag:g}")