# Python and Complex Numbers

I’ve checked the documentation for python and its’ respective modules and I noticed that there is no predefined complex function to utilize complex numbers. And since you can’t find the square root of any negative numbers, attempting to manually define i, like this:

``````import math
i = math.sqrt(-1)
``````

just returns an error. Therefore, I was wondering if there is any way to work with complex numbers in python? I’m sure they would be useful in one way or another.

There is a library which is highly recommend:

numpy

2 Likes

Awesome! Now defining i is, not only possible, but extremely easy. Albeit, it uses j instead of i, which is understandable, but still.

``````import numpy
i = numpy.lib.scimath.sqrt(-1)
print i
``````

that looks easy I never used numpy.

1 Like

Yeah, my code:

``````import numpy
i = numpy.lib.scimath.sqrt(-1)
print i
``````

results in the output of `1j`. I’ve been trying to get it so that whenever it gets a complex number, to instead print the value in terms of `i`. I’ve been messing around with it for a while, however I can’t seem to get it to work. Any suggestions?

seems this is more difficult then i thought:

https://bbolker.github.io/math1mp/notes/random/r3_sqrtminusone.html

2 Likes

Nah they’re there, without any third party libraries

``````>>> import cmath
>>> cmath.sqrt(-1)
1j
``````
``````>>> 1j + 1
(1+1j)
>>> (-1)**0.5
(6.123233995736766e-17+1j)
``````
2 Likes

numpy also gives `1j`, what is the `j` for?

1 Like

`j` is another way of writing `i`. It’s used in fields such as electrical engineering where `i` is already used to define currents.

what exactly is the purpose of your program?

1 Like

I’m making a calculator that calculates user input equations and I was trying to add complex number functionality.

for complex number functionality i would look into using a library, see what is available, i wouldn’t write your own.

Numpy is used a lot, so certain a good possibility

2 Likes

Right now, I’m just sandboxing around with python. I wasn’t going to add the complex number functionality until I myself understood how it worked within python. Which is why I was wondering if there was a way to instead of displaying the value in terms of `j`, to display the value in terms of `i`. I’ve been trying to get that to work for an hour and a half now, and no luck. I mean I’ve gotten it to go as far as turning `1j` into a string … `"i"` but once I do that, I can’t use `i` in any further computations.

Just format it differently when showing it in your program

``````>>> mynumber = 1 + 2j
>>> print(f'{mynumber.real} + {mynumber.imag}i')
1.0 + 2.0i
``````
2 Likes

So… something like this?

``````print "a + bi"
a = raw_input("a =")
b = raw_input("b =")
mynumber = "%s + %si" % (a, b)
print mynumber
``````

I wouldn’t know

1 Like

What is numpy? it sounds like Grumpy

is that really so difficult to google (or using any other search engine for that matter)?

its a python module, please don’t revive 8 months old topic for google-able questions

2 Likes