# Doing operations on complex numbers in Python

Note that when you get the output of these functions, j has the same meaning as i in this context.

### How to do addition of complex numbers in Python

``````def function(real1, imag1, real2, imag2): # defining the function, you can name it whatever you want
try: # you can catch errors with this
# the next four lines is converting the numbers to floats so you can do the operations with them
real1 = float(real1)
imag1 = float(imag1)
real2 = float(real2)
imag2 = float(imag2)
complex1 = complex(real1, imag1) # making my first complex with the given real1 and imag1
complex2 = complex(real2, imag2) # making my second complex with the given real2 and imag2
answer = complex1 + complex2 # assigning the variable answer the value of complex1 + complex2
except ValueError: # catches if someone doesn't enter numbers
return "You didn't enter numbers!" # returns "You didn't enter numbers!" to the caller code
``````

### How to do subtraction of complex numbers in Python

``````def function(real1, imag1, real2, imag2): # defining the function, you can name it whatever you want
try: # you can catch errors with this
# the next four lines is converting the numbers to floats so you can do the operations with them
real1 = float(real1)
imag1 = float(imag1)
real2 = float(real2)
imag2 = float(imag2)
complex1 = complex(real1, imag1) # making my first complex with the given real1 and imag1
complex2 = complex(real2, imag2) # making my second complex with the given real2 and imag2
answer = complex1 - complex2 # assigning the variable answer the value of complex1 0 complex2
except ValueError: # catches if someone doesn't enter numbers
return "You didn't enter numbers!" # returns "You didn't enter numbers!" to the caller code
``````

### How to do multiplication of complex numbers in Python

``````def function(real1, imag1, real2, imag2): # defining the function, you can name it whatever you want
try: # you can catch errors with this
# the next four lines is converting the numbers to floats so you can do the operations with them
real1 = float(real1)
imag1 = float(imag1)
real2 = float(real2)
imag2 = float(imag2)
complex1 = complex(real1, imag1) # making my first complex with the given real1 and imag1
complex2 = complex(real2, imag2) # making my second complex with the given real2 and imag2
answer = complex1 * complex2 # assigning the variable answer the value of complex1 * complex2
except ValueError: # catches if someone doesn't enter numbers
return "You didn't enter numbers!" # returns "You didn't enter numbers!" to the caller code
``````

### How to do division of complex numbers in Python

``````def function(real1, imag1, real2, imag2): # defining the function, you can name it whatever you want
try: # you can catch errors with this
# the next four lines is converting the numbers to floats so you can do the operations with them
real1 = float(real1)
imag1 = float(imag1)
real2 = float(real2)
imag2 = float(imag2)
complex1 = complex(real1, imag1) # making my first complex with the given real1 and imag1
complex2 = complex(real2, imag2) # making my second complex with the given real2 and imag2
answer = complex1 / complex2 # assigning the variable answer the value of complex1 / complex2