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:

  • Real Part: A real number.
  • Imaginary Part: A real number multiplied by the imaginary unit 'j' (or 'i').

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 j suffix to denote the imaginary part. Here's how you can define a complex number:

The output of the code snippet would be:

(3+4j)
<class 'complex'>

This shows how a complex number is represented and confirms its type as '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 .real and .imag attributes, respectively:

The output of the code snippet would be:

3.0
4.0

Note that the real and imaginary parts are returned as floating-point numbers.

z = 3 + 4j
print(z.real)
print(z.imag)

Basic Operations with Complex Numbers

Python supports standard arithmetic operations with complex numbers:

  • Addition: Adding two complex numbers.
  • Subtraction: Subtracting one complex number from another.
  • Multiplication: Multiplying two complex numbers.
  • Division: Dividing one complex number by another.

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 a + bj is a - bj. The .conjugate() method returns the complex conjugate:

The output of the code snippet would be:

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 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:

The output of the code snippet would be:

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

  • Use meaningful variable names: When working with complex numbers, use descriptive variable names to indicate their purpose (e.g., impedance, voltage).
  • Understand the context: Ensure you understand the mathematical context of complex numbers to apply them correctly in your application.
  • Leverage cmath module: For advanced mathematical functions (e.g., trigonometric functions, logarithmic functions) involving complex numbers, use the 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:

  • Alternating Current (AC) Circuits: Analyzing impedance, voltage, and current in AC circuits.
  • Signal Processing: Analyzing and manipulating signals in the frequency domain.
  • Quantum Mechanics: Describing the state of quantum systems.
  • Fluid Dynamics: Solving problems related to fluid flow and turbulence.

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 through cmath to work correctly with complex numbers.