Worldscope

Python complex()

Palavras-chave:

Publicado em: 06/08/2025

Understanding Complex Numbers in Python with the complex() Function

This article explores complex numbers in Python and the complex() function used to create and manipulate them. We'll delve into the function's usage, examine code examples, analyze complexity, and discuss alternative approaches for working with complex numbers in Python.

Fundamental Concepts / Prerequisites

Before diving into the complex() function, it's essential to understand the basics of complex numbers. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is the imaginary unit, satisfying the equation i² = -1. 'a' is called the real part, and 'b' is called the imaginary part. Understanding real numbers and basic arithmetic operations is also necessary.

Core Implementation/Solution


# Creating complex numbers using the complex() function

# 1. Creating a complex number with real and imaginary parts
z1 = complex(3, 4)
print(f"Complex number z1: {z1}") # Output: (3+4j)

# 2. Creating a complex number with only a real part
z2 = complex(5)
print(f"Complex number z2: {z2}") # Output: (5+0j)

# 3. Creating a complex number from a string
z3 = complex('6+7j')
print(f"Complex number z3: {z3}") # Output: (6+7j)

# 4. Accessing the real and imaginary parts
print(f"Real part of z1: {z1.real}") # Output: 3.0
print(f"Imaginary part of z1: {z1.imag}") # Output: 4.0

# 5. Performing arithmetic operations with complex numbers
z4 = z1 + z2
print(f"z1 + z2: {z4}") # Output: (8+4j)

z5 = z1 * z2
print(f"z1 * z2: {z5}") # Output: (15+20j)

#6. Using variables
real_part = 8
imag_part = 9
z6 = complex(real_part, imag_part)
print(f"Complex number z6: {z6}") # Output: (8+9j)

#7. Complex conjugate
z7 = z1.conjugate()
print(f"Complex conjugate of z1: {z7}") # Output: (3-4j)

Code Explanation

The code demonstrates various ways to create and manipulate complex numbers using Python's complex() function.

First, z1 = complex(3, 4) creates a complex number with a real part of 3 and an imaginary part of 4. The output confirms that z1 is represented as (3+4j).

Second, z2 = complex(5) creates a complex number with a real part of 5 and an imaginary part of 0. The output shows z2 as (5+0j).

Third, z3 = complex('6+7j') demonstrates creating a complex number directly from a string representation. The string must be in the form 'real+imagj' or 'real-imagj'.

Then, the code accesses the real and imaginary parts of z1 using z1.real and z1.imag respectively. These properties return the real and imaginary parts as floating-point numbers.

Arithmetic operations like addition and multiplication are performed on complex numbers (z4 = z1 + z2 and z5 = z1 * z2), showcasing Python's built-in support for complex number arithmetic.

The code demonstrates creating a complex number using variables for the real and imaginary parts. This allows for dynamic creation of complex numbers.

Finally, it shows how to calculate the complex conjugate using the `.conjugate()` method. The complex conjugate of `a + bi` is `a - bi`.

Complexity Analysis

The complex() function in Python has a time complexity of O(1), as it performs a fixed number of operations regardless of the input values. Accessing the real and imaginary parts (.real and .imag) also has a time complexity of O(1). Arithmetic operations on complex numbers (+, -, *, /) generally have a time complexity of O(1) as well, as they involve a fixed number of arithmetic operations on the real and imaginary parts.

The space complexity is also O(1) because the complex number object requires a fixed amount of memory to store the real and imaginary parts.

Alternative Approaches

While complex() is the standard and most straightforward way to create complex numbers, you can also define them using mathematical expressions directly, leveraging Python's built-in support for complex number literals. For example, z = 3 + 4j directly creates a complex number with a real part of 3 and an imaginary part of 4. This approach is functionally equivalent to using complex(3, 4) and may be more readable in some contexts. However, using the complex() function allows for dynamic creation, especially when the real and imaginary components are stored in variables.

Conclusion

The complex() function provides a simple and efficient way to create and work with complex numbers in Python. Its O(1) time and space complexity makes it suitable for a wide range of applications. Understanding how to use complex(), access real and imaginary parts, and perform arithmetic operations with complex numbers is crucial for numerical computations, signal processing, and other areas where complex numbers are essential.