Example for Complex Number in Python

Here’s an example of working with complex numbers in Python:

# Creating complex numbers
a = 3 + 2j
b = complex(4, -1)

# Addition
c = a + b
print("Addition:", c)

# Subtraction
d = a - b
print("Subtraction:", d)

# Multiplication
e = a * b
print("Multiplication:", e)

# Division
f = a / b
print("Division:", f)

# Getting real and imaginary parts
real_part = a.real
imaginary_part = a.imag
print("Real part:", real_part)
print("Imaginary part:", imaginary_part)

# Conjugate
conjugate_a = a.conjugate()
conjugate_b = b.conjugate()
print("Conjugate of a:", conjugate_a)
print("Conjugate of b:", conjugate_b)

# Absolute value (magnitude)
magnitude_a = abs(a)
magnitude_b = abs(b)
print("Magnitude of a:", magnitude_a)
print("Magnitude of b:", magnitude_b)
Code language: Python (python)

Output:

Addition: (7+1j)
Subtraction: (-1+3j)
Multiplication: (14+5j)
Division: (-0.17647058823529416+0.7058823529411764j)
Real part: 3.0
Imaginary part: 2.0
Conjugate of a: (3-2j)
Conjugate of b: (4+1j)
Magnitude of a: 3.605551275463989
Magnitude of b: 4.123105625617661
Code language: Python (python)

In this example, we create two complex numbers, a and b, and perform various operations on them, such as addition, subtraction, multiplication, and division. We also demonstrate how to access the real and imaginary parts of a complex number, compute its conjugate, and calculate its absolute value (magnitude).

How do you use complex () in Python?

In Python, the complex() function is used to create a complex number. It takes two arguments: the real part and the imaginary part of the complex number. The function returns a complex number object.

Here’s the syntax for using the complex() function:

z = complex(real, imag)Code language: Python (python)

where:

  • real is the real part of the complex number (a float or an integer).
  • imag is the imaginary part of the complex number (a float or an integer).

Here are a few examples of using the complex() function:

# Creating complex numbers using complex()
a = complex(3, 2)    # a = 3 + 2j
b = complex(-1, 4)   # b = -1 + 4j
c = complex(0, -2.5) # c = -2.5j

# Printing the complex numbers
print(a)  # (3+2j)
print(b)  # (-1+4j)
print(c)  # (-2.5j)
Code language: Python (python)

In the examples above, we use the complex() function to create complex numbers with different real and imaginary parts. We then print the complex numbers using the print() function. The output shows the complex numbers in the standard format (real+imagj).

How do you input a complex number in Python?


To input a complex number in Python, you can use the input() function to get user input as a string, and then convert it to a complex number using the complex() function.

Here’s an example:

# Get user input as a string
input_str = input("Enter a complex number (in the form a+bj): ")

# Convert the input string to a complex number
complex_num = complex(input_str)

# Print the complex number
print("Complex number:", complex_num)
Code language: Python (python)

When you run this code, it will prompt you to enter a complex number in the form a+bj, where a represents the real part and b represents the imaginary part. After you enter the complex number and press Enter, the program will convert the input string to a complex number using the complex() function and store it in the variable complex_num. Finally, it will print the complex number.

Here’s an example of running the code and entering a complex number:

Enter a complex number (in the form a+bj): 3+2j
Complex number: (3+2j)Code language: Python (python)

In this example, we entered 3+2j as the input, and the program converted it to the complex number (3+2j).

How do you create a complex number class in Python?

To create a complex number class in Python, you can define a new class and implement the required functionality for complex numbers. Here’s an example of a basic complex number class:

class ComplexNumber:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag

    def __repr__(self):
        return f"({self.real}+{self.imag}j)"

    def __add__(self, other):
        if isinstance(other, ComplexNumber):
            real_sum = self.real + other.real
            imag_sum = self.imag + other.imag
            return ComplexNumber(real_sum, imag_sum)
        else:
            raise TypeError("Unsupported operand type for +")

    def __sub__(self, other):
        if isinstance(other, ComplexNumber):
            real_diff = self.real - other.real
            imag_diff = self.imag - other.imag
            return ComplexNumber(real_diff, imag_diff)
        else:
            raise TypeError("Unsupported operand type for -")

    def __mul__(self, other):
        if isinstance(other, ComplexNumber):
            real_prod = self.real * other.real - self.imag * other.imag
            imag_prod = self.real * other.imag + self.imag * other.real
            return ComplexNumber(real_prod, imag_prod)
        else:
            raise TypeError("Unsupported operand type for *")

    def __truediv__(self, other):
        if isinstance(other, ComplexNumber):
            denominator = other.real ** 2 + other.imag ** 2
            real_quot = (self.real * other.real + self.imag * other.imag) / denominator
            imag_quot = (self.imag * other.real - self.real * other.imag) / denominator
            return ComplexNumber(real_quot, imag_quot)
        else:
            raise TypeError("Unsupported operand type for /")

    def conjugate(self):
        return ComplexNumber(self.real, -self.imag)

    def magnitude(self):
        return (self.real ** 2 + self.imag ** 2) ** 0.5
Code language: Python (python)

In this example, we define a class named ComplexNumber that represents a complex number. The class has an __init__ method to initialize the real and imaginary parts of the complex number.

We also implement several special methods such as __repr__, __add__, __sub__, __mul__, and __truediv__ to enable addition, subtraction, multiplication, and division operations on complex numbers.

The class also includes methods for computing the conjugate (conjugate()) and magnitude (magnitude()) of a complex number.

Here’s an example of using the ComplexNumber class:

# Create complex numbers
a = ComplexNumber(3, 2)
b = ComplexNumber(4, -1)

# Addition
c = a + b
print("Addition:", c)

# Subtraction
d = a - b
print("Subtraction:", d)

# Multiplication
e = a * b
print("Multiplication:", e)

# Division
f = a / b
print("Division:", f)

# Conjugate
conjugate_a = a.conjugate()
conjugate_b = b.conjugate()
print("Conjugate of a:", conjugate_a)
print("Conjugate of b:", conjugate_b)

# Magnitude
magnitude_a = a.magnitude()
magnitude_b = b.magnitude()
print("Magnitude of a:", magnitude_a)
print("Magnitude of b:", magnitude_b)
Code language: Python (python)

Output:

Addition: (7+1j)
Subtraction: (-1+3j)
Multiplication: (14+5j)
Division
Code language: Python (python)

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

    View all posts

Leave a Comment