Hi friends, Welcome back to DailycodeHub.
When learning Python programming, we usually start with numeric data types such as integers and floating-point numbers. However, Python also supports another powerful numeric type known as complex numbers.
Complex numbers are widely used in mathematics, electrical engineering, signal processing, data science and scientific computing. These numbers allow programmers to represent values that contain both real and imaginary components.
In Python complex numbers are written using the letter j to represent the imaginary part.
Example :
code :
# Create a complex number
z = 3 + 4j
# Print the complex number
print(z)
Output :
(3+4j)
Here :
- 3 represents the real part
- 4j represents the imaginary part
While many beginners simply perform arithmetic operations with complex numbers, Python actually provides useful built-in attributes and methods that help programmers access and manipulate complex numbers easily.
Understanding these features is important when working with scientific applications, mathematical simulations and advanced numerical calculations.
In this article we will clearly understand:
- What complex numbers are in Python
- Important complex numbers attributes and methods
- How each method works with examples
- Where complex numbers are used in real programming
So let us begin step by step.
 |
| COMPLEX NUMBERS METHODS |
Table of contents :
- What are complex numbers in Python?
- Creating complex numbers in python
- Python complex number attributes and methods
- real attribute
- imag attribute
- conjugate ( ) method
- Example programs
- Practice Questions
- Python interview Questions
- Frequently Asked Questions
- Conclusion
What are complex numbers in Python?
A complex number is a number that contains both real and imaginary components.
Mathematically , complex numbers are written in the form :
a + bj
Where :
- a = real part
- b = imaginary part
- j = imaginary unit
Example:
5 + 2j
In Python, complex numbers belong to the complex class, which means they support certain attributes and methods that helps us work with them easily.
Unlike strings or lists, complex numbers do not have many methods. However Python provides three important attributes and methods:
real
These features allow programmers to extract values and perform mathematical transformations.
Let us understand each of them one by one.
Creating complex numbers in Python
Python allows us to create complex numbers in two simple ways.
Method 1 : Direct Representation
We can directly write complex numbers using j.
Example :
code :
# Create a complex number
z = 6 + 9j
# Print the complex number
print(z)
Output :
(6+9j)
Method 2 : Using the complex( ) function
Python also provides the complex( ) constructor.
Syntax :
complex(real, imaginary)
Example :
Code :
# Create a complex number using complex() function
z = complex(6, 9)
# Print the complex number
print(z)
(6+9j)
This method is useful when the values come from user input or program variables.
Python complex Numbers Attributes and methods
Python mainly provides two attributes and one method for complex numbers.
| Attribute / Method |
Description |
| real |
Returns the real part of the complex number |
| imag |
Returns the imaginary part |
| conjugate() |
Returns the complex conjugate |
Now let us understand each one in detail.
1. real attribute in Python
What does the real attribute do ?
The real attribute returns the real components of a complex number.
Example complex number:
7 + 5j
Real part =7
Syntax :
complex_number.real
Example Program :
Code :
# Create a complex number
z = 7 + 5j
# Print the real part of the complex number
print(z.real)
7.0
 |
| working of real attribute in python complex numbers |
Explanation :
The real attribute extracts the real portion of the complex number and returns it as a floating-point value.
Where is it useful?
This attribute is useful in :
- Mathematical calculations
- Scientific simulations
- Data analysis
- Engineering applications
2. imag attribute in Python
What does imag do?
The imag attribute returns the imaginary part of a complex number.
Example complex number:
7 + 5j
imaginary part = 5
Syntax :
complex_number.imag
Example program :
Code :
# Create a complex number
z = 7 + 5j
# Print imaginary part of the complex number
print(z.imag)
Output :
5.0
 |
| working of imag attribute in python complex numbers |
Explanation :
The imag attribute extracts the imaginary value and returns it as a floating-point number.
Real-world applications
Imaginary numbers are commonly used in :
- Electrical circuit analysis
- Signal processing
- Communication systems
- Physics calculations
3. conjugate( ) method in Python :
What does conjugate ( ) do ?
The conjugate( ) method returns the complex conjugate of a number.
A complex conjugate is obtained by changing the sign of the imaginary part.
Example :
3 + 4j → 3 − 4j
Syntax :
complex_number.conjugate( )
Example program:
Code :
# Create a complex number
z = 3 + 4j
# Print conjugate of the complex number
print(z.conjugate())
Output :
(3-4j)
 |
| working of conjugate method in python complex numbers |
Explanation :
The imaginary part changes its sign from +4j to -4j, while the real part remains the same.
Applications :
Complex conjugates are widely used in :
Electrical engineering
Fourier transforms
Signal processing
Quantum mechanics
Control systems
Example Program using complex number methods:
Below is a simple Python Program that demonstrates how to use complex numbers attributes.
Code :
# Create a complex number
z = 8 + 6j
# Print complex number
print("Complex Number:", z)
# Print real part
print("Real Part:", z.real)
# Print imaginary part
print("Imaginary Part:", z.imag)
# Print conjugate of complex number
print("Conjugate:", z.conjugate())
Output :
Complex Number: (8+6j)
Real Part: 8.0
Imaginary Part: 6.0
Conjugate: (8-6j)
This program shows how Python can easily extract and manipulate different parts of a complex number.
Practice Questions:
Try solving the following questions to strengthen your understanding.
- Write a Python program to print the real part of 9 + 7j.
- Write a program to print the imaginary part of 5 + 3j.
- Write a Python program to find the conjugate of 8 + 4j.
- Create a complex number using complex( ) and print its real and imaginary parts.
- What will be the output of the following code ?
Code :
# Create a complex number
z = 6 + 2j
# Print conjugate of the complex number
print(z.conjugate())
Python complex numbers interview Questions :
- What is a complex number in Python ?
- What attributes are available for complex numbers?
- What does the conjugate ( ) method do ?
- How do you create a complex number using the complex( ) function?
- What is the difference between real and imag attributes?
These questions often appear in Python interviews and Programming assessments.
Frequently Asked Questions (FAQ's) :
What is a complex numbers in Python?
A complex number is a number that contains both real and imaginary components.
Example :
4 + 3j
How many complex number methods exist in Python ?
Python mainly provides two attributes and one method
What does the conjugate( ) method return?
It returns the complex conjugate, meaning the imaginary part changes its sign.
Example :
5 + 2j → 5 − 2j
Where are complex numbers used?
Complex numbers are used in :
- Electrical engineering
- Signal processing
- Scientific computing
- Physics simulations
Related articles :
Python float methods explained with examples
Python integer methods guide
Conclusion :
Complex numbers are an important part of Python numeric system and allow programmers to work with imaginary values and advanced mathematical calculations.
Although Python provides only a few attributes and methods for complex numbers understanding real, imag and conjugate( ) helps developers perform complex mathematical operations efficiently.
By practicing these simple examples and programs beginners can easily understand how Python handles complex numbers and how they are used in scientific and engineering applications.
Keep practicing and experimenting with these methods to strengthen your Python fundamentals.
Stay connected with DailyCodeHub for more Python tutorials, coding interview questions, and programming tips.