Nearby lessons
17 of 159Python - 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
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
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
Arithmetic Operations on Complex Numbers
Python supports arithmetic operations on complex numbers.
Addition
Subtraction
Multiplication
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
Checking the Data Type
The type() function returns the data type of a value.
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) |
📝 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 QuestionsProgress: 0 / 8