Python tutorials > Core Python Fundamentals > Data Types and Variables > What are complex numbers?
What are complex numbers?
Complex numbers are a fundamental concept in mathematics and are also supported as a built-in data type in Python. They extend the real number system by including an imaginary unit, denoted as 'j' in Python (or 'i' in mathematics), which is defined as the square root of -1. This tutorial will explain what complex numbers are, how they are represented in Python, and how to perform basic operations with them.
Understanding Complex Numbers
A complex number is composed of two parts: A complex number is generally written in the form
a + bj
, where 'a' is the real part and 'b' is the imaginary part.
Representing Complex Numbers in Python
In Python, complex numbers are created using the The output of the code snippet would be: This shows how a complex number is represented and confirms its type as 'complex'.j
suffix to denote the imaginary part. Here's how you can define a complex number:(3+4j)
<class 'complex'>
z = 3 + 4j
print(z)
print(type(z))
Accessing Real and Imaginary Parts
You can access the real and imaginary parts of a complex number using the The output of the code snippet would be: Note that the real and imaginary parts are returned as floating-point numbers..real
and .imag
attributes, respectively:3.0
4.0
z = 3 + 4j
print(z.real)
print(z.imag)
Basic Operations with Complex Numbers
Python supports standard arithmetic operations with complex numbers: The output of the code snippet would be:
Addition: (4+2j)
Subtraction: (2+6j)
Multiplication: (11-2j)
Division: (-0.2+1.6j)
z1 = 3 + 4j
z2 = 1 - 2j
# Addition
addition = z1 + z2
print(f"Addition: {addition}")
# Subtraction
subtraction = z1 - z2
print(f"Subtraction: {subtraction}")
# Multiplication
multiplication = z1 * z2
print(f"Multiplication: {multiplication}")
# Division
division = z1 / z2
print(f"Division: {division}")
Conjugate of a Complex Number
The conjugate of a complex number The output of the code snippet would be:a + bj
is a - bj
. The .conjugate()
method returns the complex conjugate:Conjugate of (3+4j): (3-4j)
z = 3 + 4j
conjugate = z.conjugate()
print(f"Conjugate of {z}: {conjugate}")
Magnitude (Absolute Value) of a Complex Number
The magnitude (or absolute value) of a complex number The output of the code snippet would be:a + bj
is calculated as √(a² + b²)
. Python's built-in abs()
function can be used for this purpose. The phase can be calculated with the cmath.phase
function:Magnitude of (3+4j): 5.0
Phase of (3+4j): 0.9272952180016122
import cmath
z = 3 + 4j
magnitude = abs(z)
print(f"Magnitude of {z}: {magnitude}")
phase = cmath.phase(z)
print(f"Phase of {z}: {phase}")
Real-Life Use Case: Electrical Engineering
Complex numbers are extensively used in electrical engineering, particularly in AC circuit analysis. Impedance, which is the opposition to the flow of alternating current, is represented as a complex number. The real part represents resistance, and the imaginary part represents reactance (due to inductors and capacitors). Calculations involving voltage, current, and impedance in AC circuits often involve complex number arithmetic.
Best Practices
impedance
, voltage
).cmath
module.
Interview Tip
Be prepared to explain what complex numbers are and their applications. You might be asked to perform basic operations with complex numbers or explain how they are used in specific domains like signal processing or electrical engineering. Demonstrating a clear understanding of the real and imaginary components and their mathematical properties is key.
When to Use Them
Use complex numbers when dealing with problems that involve:
Memory Footprint
Complex numbers in Python typically consume more memory than integers or floats because they store two floating-point numbers (real and imaginary parts). Be mindful of memory usage when working with a large number of complex numbers, especially in performance-critical applications. The specific memory footprint can vary depending on the Python implementation and the system architecture.
FAQ
-
Why use 'j' instead of 'i' for the imaginary unit in Python?
In mathematics, 'i' is commonly used to represent the imaginary unit. However, in some engineering fields (particularly electrical engineering), 'i' is often used to represent current. To avoid confusion, Python uses 'j' to represent the imaginary unit.
-
Can I convert a complex number to a real number?
You can convert a complex number to a real number only if its imaginary part is zero. You can use the
.real
attribute to extract the real part, but this will still be a floating-point number. -
Are there any limitations to using complex numbers in Python?
While Python provides robust support for complex numbers, some operations or functions might require special handling or the use of the
cmath
module. For example, certain mathematical functions (like trigonometric or logarithmic functions) need to be accessed throughcmath
to work correctly with complex numbers.