Python Complex Numbers

Alright ! Let’s talk about Python complex numbers in simpler terms. In math, complex numbers are just regular old numbers with an imaginary part thrown in for good measure. For example, let’s say you have the number 3 plus your standard i (which stands for “imaginary unit”) squared. That would look like this:

3 + 4i

In Python, we can handle these bad boys by creating a new object called `complex` and passing it the real part followed by the imaginary part separated by a plus sign (just like we did with our example). Here’s what that would look like in code:

# Importing the cmath library for complex numbers
import cmath

# Creating a new complex object called "num" with a value of 3 + 4j
num = complex(3, 4)

# Printing the type of the "num" object to check if it is a complex number
print(type(num))

# Output: <class 'complex'>

Output: ``

And thats that! You’ve successfully created your first Python complex number. But what can we do with these things? Well, let’s take a look at some of the most common operations that involve complex numbers in Python.

1. Addition: To add two complex numbers together, simply use the `+` operator just like you would for regular numbers. For example:

# Creating two complex numbers using the `complex()` function and assigning them to variables
num1 = complex(2, 3) # num1 is assigned a value of 2 plus i times 3
num2 = complex(5, -7) # num2 is assigned a value of 5 minus i times 7

# Adding the two complex numbers together using the `+` operator and assigning the result to a variable
result = num1 + num2 

# Printing out the result
print(result) # Output: (7-4j)

# The `complex()` function takes two arguments, the real and imaginary parts of a complex number
# The `+` operator is used to add two complex numbers together
# The `print()` function is used to display the result on the screen

Output: `(7.0-4.0j)`

2. Subtraction: To subtract one complex number from another, simply use the `-` operator just like you would for regular numbers. For example:

# Creating a complex number with real part 5 and imaginary part 3
num1 = 5 + 3j 
# Creating a complex number with real part 2 and imaginary part -7
num2 = 2 - 7j 
# Subtracting num2 from num1 using the `-` operator
result = num1 - num2 
# Printing out the result
print(result) 

Output: `(3.0+5.0j)`

3. Multiplication: To multiply two complex numbers together, simply use the `*` operator just like you would for regular numbers. For example:

# Creating a complex number with a real part of 2 and an imaginary part of 3
num1 = 2 + 3j
# Creating a complex number with a real part of 5 and an imaginary part of 7
num2 = 5 + 7j
# Multiplying the two complex numbers together using the `*` operator
result = num1 * num2
# Printing out the result
print(result) # Output: (3.0+5.0j)

Output: `(-38.0+69.0j)`

4. Division: To divide one complex number by another, simply use the `/` operator just like you would for regular numbers. For example:

# Creating a complex number with real part 5 and imaginary part 3
num1 = 5 + 3j 
# Creating a complex number with real part 2 and imaginary part -7
num2 = 2 - 7j 
# Dividing num1 by num2 using the `/` operator
result = num1 / num2 
# Printing the result
print(result) 

Output: `(-0.8333333333333334-0.5j)`

And that’s it! You now have a basic understanding of how to work with complex numbers in Python. Of course, there are many more operations and functions available for these bad boys (like finding the modulus or argument), but we’ll save those for another day. Later !

SICORPS