Introduction to the complex()
Function
In Python, the complex()
function is a built-in function that creates a complex number. A complex number is a number that consists of both a real and an imaginary part. It is often used in mathematical and scientific computations where real numbers alone may not suffice.
The syntax of the complex()
function is as follows:
complex([real[, imag]])
Here, real
is the real part of the complex number, and imag
is the imaginary part. Both real
and imag
are optional parameters. If both are omitted, the function returns a complex number with both real and imaginary parts equal to 0.
Creating Complex Numbers
Using Only the Real Part
You can create a complex number using the complex()
function by providing only the real part. In this case, the imaginary part will be assumed to be 0. Let’s see an example:
# Create a complex number with only the real part
real_part = 5
complex_num = complex(real_part)
print("Complex Number:", complex_num)
print("Real Part:", complex_num.real)
print("Imaginary Part:", complex_num.imag)
Output:
Complex Number: (5+0j)
Real Part: 5.0
Imaginary Part: 0.0
In this example, we provided the real part 5
to the complex()
function, and it created a complex number (5+0j)
.
Using Both Real and Imaginary Parts
You can also create a complex number by providing both the real and imaginary parts as arguments to the complex()
function. Let’s take a look at an example:
# Create a complex number with both real and imaginary parts
real_part = 3
imaginary_part = 4
complex_num = complex(real_part, imaginary_part)
print("Complex Number:", complex_num)
print("Real Part:", complex_num.real)
print("Imaginary Part:", complex_num.imag)
Output:
Complex Number: (3+4j)
Real Part: 3.0
Imaginary Part: 4.0
In this example, we provided the real part 3
and the imaginary part 4
to the complex()
function, resulting in the complex number (3+4j)
.
Using Polar Coordinates
The complex()
function also allows you to create a complex number using polar coordinates. Polar coordinates consist of a magnitude (distance from the origin) and an angle (in radians) that the complex number makes with the positive real axis. You can convert polar coordinates to a complex number using the following formula:
complex_num = magnitude * (cos(angle) + sin(angle)*j)
Let’s illustrate this with an example:
import math
# Create a complex number using polar coordinates
magnitude = 2
angle_degrees = 45
angle_radians = math.radians(angle_degrees)
real_part = magnitude * math.cos(angle_radians)
imaginary_part = magnitude * math.sin(angle_radians)
complex_num = complex(real_part, imaginary_part)
print("Complex Number:", complex_num)
print("Real Part:", complex_num.real)
print("Imaginary Part:", complex_num.imag)
Output:
Complex Number: (1.4142135623730951+1.4142135623730951j)
Real Part: 1.4142135623730951
Imaginary Part: 1.4142135623730951
In this example, we created a complex number using polar coordinates with a magnitude of 2
and an angle of 45
degrees. The resulting complex number is approximately (1.4142135623730951+1.4142135623730951j)
.
Arithmetic Operations with Complex Numbers
Python provides support for performing arithmetic operations with complex numbers, just like you would with real numbers. Let’s explore some of these operations using the complex numbers we created earlier.
Addition
You can add two complex numbers using the +
operator. The real parts and imaginary parts are added separately. Let’s see an example:
# Adding two complex numbers
complex_num1 = complex(2, 3)
complex_num2 = complex(4, 5)
sum_complex = complex_num1 + complex_num2
print("Complex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("Sum:", sum_complex)
Output:
Complex Number 1: (2+3j)
Complex Number 2: (4+5j)
Sum: (6+8j)
In this example, we added the complex numbers (2+3j)
and (4+5j)
to get the sum (6+8j)
.
Subtraction
Subtraction of complex numbers is also straightforward using the -
operator. The real parts and imaginary parts are subtracted separately. Here’s an example:
# Subtracting one complex number from another
complex_num1 = complex(8, 12)
complex_num2 = complex(3, 5)
difference_complex = complex_num1 - complex_num2
print("Complex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("Difference:", difference_complex)
Output:
Complex Number 1: (8+12j)
Complex Number 2: (3+5j)
Difference: (5+7j)
In this example, we subtracted the complex number (3+5j)
from (8+12j)
to obtain the difference (5+7j)
.
Multiplication
Multiplying two complex numbers is achieved using the *
operator. The multiplication follows the distributive property. Let’s take a look:
# Multiplying two complex numbers
complex_num1 = complex(2, 3)
complex_num2 = complex(4, 5)
product_complex = complex_num1 * complex_num2
print("Complex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("Product:", product_complex)
Output:
Complex Number 1: (2+3j)
Complex Number 2: (4+5j)
Product: (-7+22j)
In this example, we multiplied the complex numbers (2+3j)
and (4+5j)
to get the product (-7+22j)
.
Division
Division of complex numbers is performed using the /
operator. To divide two complex numbers, multiply the numerator and denominator by the conjugate of the denominator. The conjugate of a complex number a + bj
is a - bj
. Here’s how it works:
# Dividing one complex number by another
complex_num1 = complex(6, 8)
complex_num2 = complex(2, 3)
quotient_complex = complex_num1 / complex_num2
print("Complex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("Quotient:", quotient_complex)
Output:
Complex Number 1: (6+8j)
Complex Number 2: (2+3j)
Quotient: (2.769230769230769+0.3076923076923077j)
In this example, we divided the complex number (6+8j)
by (2+3j)
to obtain the quotient (2.769230769230769+0.3076923076923077j)
.
Conclusion
In this tutorial, we explored the complex()
function in Python, which is used to create complex numbers. We covered how to create complex numbers using both real and imaginary parts, as well as how to use polar coordinates to create complex numbers. Additionally, we delved into performing arithmetic operations such as addition, subtraction, multiplication, and division with complex numbers.
Complex numbers are a fundamental concept in mathematics and have various applications in fields such as physics, engineering, and computer science. Understanding how to work with complex numbers in Python will equip you with valuable skills for solving a wide range of problems.