Nearby lessons
17 of 108Python - complex Data Type
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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
SyntaxError
Arithmetic Operations on Complex Numbers
Python supports arithmetic operations on complex numbers.
Addition
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(30+4j)
Subtraction
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(10+3j)
Multiplication
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
(-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
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10.5 3.6
Checking the Data Type
The type() function returns the data type of a value.
🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
realandimagto access different parts of a complex number.
Key Points
- The
complexdata 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
realandimagto access the real and imaginary parts.
Quick Summary
| Feature | Description |
|---|---|
| Data Type | complex |
| Purpose | Represents complex numbers |
| Format | a + bj |
| Real Part | Supports decimal, binary, octal, and hexadecimal |
| Imaginary Part | Decimal only |
| Attributes | real, imag |
| Mutable | No (Immutable) |
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8
Keep Going!Python - bool Data Type