Complex Numbers in Python

Overview

In Python, complex numbers are a built-in data type that allows for the representation and manipulation of numbers with both real and imaginary components. A complex number is automatically created when an expression of the form nj is encountered, where n is a number and j is the imaginary unit. The imaginary unit j is used to denote the square root of -1. Complex numbers in Python are represented as a single object, and their real and imaginary parts can be accessed using the real and imag attributes.

Examples of Complex Numbers

Complex numbers can be manipulated using standard arithmetic operations such as addition, subtraction, multiplication, and division. Below are some examples of operations involving complex numbers:

# [Creating a complex number](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/118)
(3+2j)

# [Subtraction of complex numbers](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/99)
3 + 2j - (4+4j)
# [Result: (-1-2j)](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/99)

# [Multiplication of complex numbers](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/99)
(1+2j) * (3+4j)
# [Result: (-5+10j)](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/99)

# [Multiplication involving the imaginary unit](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/99)
1j * 1j
# [Result: (-1+0j)](https://livebook.manning.com/book/the-quick-python-book-fourth-edition/chapter-4/118)

In these examples, you can see how complex numbers are created and manipulated using basic arithmetic operations. The results are also complex numbers, with both real and imaginary parts.

Advanced Functions for Complex Numbers

Python provides a specialized module called cmath for performing advanced mathematical operations on complex numbers. The cmath module includes a variety of functions that are specifically designed to work with complex numbers. Some of the functions available in the cmath module include:

  • Trigonometric functions: acos, acosh, asin, asinh, atan, atanh, cos, cosh, sin, sinh, tan, tanh
  • Exponential and logarithmic functions: exp, log, log10
  • Constants: e, pi
  • Square root function: sqrt

These functions allow for complex mathematical computations involving complex numbers, making Python a powerful tool for scientific and engineering applications that require complex number calculations.

FAQ (Frequently asked questions)

How can you access the real and imaginary parts of a complex number in Python?

Can complex numbers in Python be manipulated using arithmetic operations?

What is the result of subtracting (4+4j) from (3+2j) in Python?

What is the result of multiplying (1+2j) by (3+4j) in Python?

What is the result of multiplying 1j by 1j in Python?

What module provides advanced functions for complex numbers in Python?