Nearby lessons

17 of 159

Python - complex Data Type

📌 What You Will Learn
  • Define the complex data type and the form a + bj
  • Identify the real and imaginary parts of a complex number
  • Understand the number systems allowed for the real and imaginary parts
  • Perform addition, subtraction, and multiplication on complex numbers
  • Access the real and imaginary parts using the real and imag attributes

Introduction

The complex data type is used to represent complex numbers.

A complex number is written in the form a + bj.

  • a = Real Part
  • b = Imaginary Part
  • j = Imaginary Unit (√-1)

Structure of a Complex Number

Part Description
a Real Part
b Imaginary Part
j Imaginary Unit (√-1)

Examples of Complex Numbers

🐍Code Cell
1a = 3 + 5j
2b = 10 + 5.5j
3c = 0.5 + 0.1j
4 
5print(a)
6print(b)
7print(c)
Output
(3+5j)
(10+5.5j)
(0.5+0.1j)

Real Part of a Complex Number

The real part of a complex number can be represented using different number systems.

  • Decimal
  • Binary
  • Octal
  • Hexadecimal

Example

🐍Code Cell
1a = 0B11 + 5j
2 
3print(a)
Output
(3+5j)

Imaginary Part of a Complex Number

The imaginary part must always be written in decimal form.

Binary, octal, and hexadecimal values are not allowed in the imaginary part.

Invalid Example

🐍Code Cell
1a = 3 + 0B11j
Output
SyntaxError

Arithmetic Operations on Complex Numbers

Python supports arithmetic operations on complex numbers.

Addition

🐍Code Cell
1a = 10 + 1.5j
2b = 20 + 2.5j
3 
4c = a + b
5 
6print(c)
7print(type(c))
Output
(30+4j)

Subtraction

🐍Code Cell
1a = 15 + 5j
2b = 5 + 2j
3 
4print(a - b)
Output
(10+3j)

Multiplication

🐍Code Cell
1a = 2 + 3j
2b = 1 + 2j
3 
4print(a * b)
Output
(-4+7j)

Accessing Real and Imaginary Parts

Python provides two built-in attributes for complex numbers.

Attribute Purpose
real Returns the real part.
imag Returns the imaginary part.

Example

🐍Code Cell
1c = 10.5 + 3.6j
2 
3print(c.real)
4print(c.imag)
Output
10.5
3.6

Checking the Data Type

The type() function returns the data type of a value.

🐍Code Cell
1a = 5 + 2j
2 
3print(type(a))
Output
No output captured.

Important Mathematical Note

For complex numbers:

j² = -1

Applications of Complex Numbers

Complex numbers are commonly used in:

  • Scientific Applications
  • Electrical Engineering
  • Electronics
  • Signal Processing
  • Electrical Circuits
  • Mathematical Calculations

Difference Between int, float, and complex

Data Type Example Description
int 10 Whole numbers
float 10.5 Decimal numbers
complex 10+5j Complex numbers

Important Points

  • Complex numbers are immutable.
  • The real part supports decimal, binary, octal, and hexadecimal values.
  • The imaginary part supports only decimal values.
  • Python supports arithmetic operations on complex numbers.
  • Use real and imag to access different parts of a complex number.

Key Points

  • The complex data type represents complex numbers.
  • A complex number is written in the form a + bj.
  • The real part can use different number systems.
  • The imaginary part must always be in decimal form.
  • Use type() to check the data type.
  • Use real and imag to access the real and imaginary parts.

Quick Summary

Feature Description
Data Typecomplex
PurposeRepresents complex numbers
Formata + bj
Real PartSupports decimal, binary, octal, and hexadecimal
Imaginary PartDecimal only
Attributesreal, imag
MutableNo (Immutable)
📝 Key Takeaways
  • A complex number is written in the form a + bj
  • The real part supports decimal, binary, octal, and hexadecimal values
  • The imaginary part must always be in decimal form
  • Python supports arithmetic operations on complex numbers
  • Use the real and imag attributes to access parts of a complex number

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8