• Home
  • 3.3 Complex Numbers

3.3 Complex Numbers

View Categories

3.3 Complex Numbers

4 min read

A complex number is a number that consists of two parts:

  • A real part
  • An imaginary part

In Python, complex numbers are represented using the built-in complex data type.

The imaginary part is written using the letter j.

Complex numbers are widely used in engineering, electronics, signal processing, control systems, physics, and mathematics.

A complex number has the general form:

a + bj

where:

  • a is the real part.
  • b is the imaginary part.
  • j represents the square root of -1.

Syntax #

variable_name = real_part + imaginary_partj

Components #

Component Description
variable_name Name of the variable.
real_part The real component of the number.
imaginary_part The coefficient of the imaginary part.
j Represents the imaginary unit.

Example 1: Creating a Complex Number #

main.py

number = 3 + 4j

print(number)
print(type(number))

Output #

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

Explanation #

  • 3 is the real part.
  • 4j is the imaginary part.
  • Python stores the value as a complex number.
  • The type() function confirms the data type.

Example 2: Complex Number with Negative Imaginary Part #

main.py

number = 5 - 2j

print(number)

Output #

(5-2j)

Explanation #

  • The real part is 5.
  • The imaginary part is -2j.
  • Complex numbers may contain positive or negative imaginary values.

Example 3: Accessing Real and Imaginary Parts #

main.py

number = 6 + 8j

print(number.real)
print(number.imag)

Output #

6.0
8.0

Explanation #

  • Every complex number provides two attributes:
    • .real
    • .imag
  • These return the real and imaginary parts as floating-point numbers.

Example 4: Addition of Complex Numbers #

main.py

a = 2 + 3j
b = 4 + 5j

print(a + b)

Output #

(6+8j)

Explanation #

  • Python adds the real parts separately.
  • It also adds the imaginary parts separately.
  • The result is another complex number.

Example 5: Subtraction of Complex Numbers #

main.py

a = 8 + 6j
b = 3 + 2j

print(a - b)

Output #

(5+4j)

Explanation #

  • The real parts are subtracted.
  • The imaginary parts are also subtracted.
  • The result remains a complex number.

Example 6: Multiplication of Complex Numbers #

main.py

a = 2 + 3j
b = 1 + 2j

print(a * b)

Output #

(-4+7j)

Explanation #

  • Python performs complex multiplication automatically.
  • Both the real and imaginary parts are considered during the calculation.
  • The result is another complex number.

Example 7: Creating a Complex Number Using complex() #

main.py

number = complex(5, 7)

print(number)

Output #

(5+7j)

Explanation #

  • The complex() function creates a complex number.
  • The first argument is the real part.
  • The second argument is the imaginary part.

Example 8: Type of Real and Imaginary Parts #

main.py

number = 4 + 9j

print(type(number.real))
print(type(number.imag))

Output #

<class 'float'>
<class 'float'>

Explanation #

  • Even when integers are used to create a complex number, the .real and .imag attributes return floating-point values.

Example 9: Equality Comparison #

main.py

a = 3 + 4j
b = 3 + 4j

print(a == b)

Output #

True

Explanation #

  • Both complex numbers have identical real and imaginary parts.
  • Therefore, they are considered equal.

Example 10: Printing Real and Imaginary Parts Separately #

main.py

number = 10 + 15j

print("Real Part:", number.real)
print("Imaginary Part:", number.imag)

Output #

Real Part: 10.0
Imaginary Part: 15.0

Explanation #

  • The .real attribute returns the real component.
  • The .imag attribute returns the imaginary component.
  • These attributes make it easy to access individual parts of a complex number.

Common Mistakes #

1. Using i Instead of j #

Incorrect

number = 3 + 4i

Output #

SyntaxError: invalid decimal literal

Reason

Python uses j to represent the imaginary unit, not i.

Correct

number = 3 + 4j

2. Forgetting the Imaginary Unit #

Incorrect

number = 3 + 4

Output #

7

Reason

Without j, Python performs ordinary integer addition.

Correct

number = 3 + 4j

3. Expecting a Complex Number to be an Integer #

Incorrect

number = 2 + 3j

print(type(number))

Incorrect Expectation #

<class 'int'>

Actual Output #

<class 'complex'>

Reason

Values containing an imaginary part are stored as complex numbers.


4. Trying to Compare Complex Numbers Using < or > #

Incorrect

a = 3 + 2j
b = 4 + 5j

print(a > b)

Output #

TypeError: '>' not supported between instances of 'complex' and 'complex'

Reason

Complex numbers support equality (==) and inequality (!=), but they cannot be ordered using relational operators such as <, >, <=, or >=.


Best Practices #

  • Use j to represent the imaginary unit in Python.
  • Use .real and .imag to access the individual parts of a complex number.
  • Use the complex() function when the real and imaginary parts are stored separately.
  • Remember that complex numbers are mainly used in scientific, engineering, and mathematical applications.
  • Use equality operators (==, !=) when comparing complex numbers.

Key Points to Remember #

  • Python provides the complex data type for representing complex numbers.
  • A complex number consists of a real part and an imaginary part.
  • Python uses j as the imaginary unit.
  • The .real and .imag attributes return the real and imaginary parts.
  • Complex numbers support arithmetic operations such as addition, subtraction, multiplication, and division.
  • Use complex(real, imaginary) to create a complex number programmatically.
  • Complex numbers cannot be compared using relational operators such as < or >.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *