Beginner Level
Intermediate Level
Advanced Level
Introduction
The complex data type in python is unique and powerful because it enables programmers to work with complex numbers, which are a combination of real and imaginary parts. Complex numbers are often used in fields like mathematics, engineering, and science, making it a crucial data type for scientific computing and academic research. In this course, we will dive deep into complex data types and learn how to use them effectively in Python programming.
Table of Contents :
- Complex data type in Python
- Creating a complex variable
- Creating an instance of complex class
Complex data type in Python
- In Mathematics a complex number is a number which has two components -
- a real number and
- an imaginary component.
- To declare a complex value we need to use the
a+bj
form. - The real part of a complex number can be an
- The imaginary part of a complex number can be an integer or float only.
- In Python complex variable is an instance of
complex
class. - Usually the complex type is used for scientific and engineering purposes.
- A complex variable can be created in two ways in Python
- Assigning a complex literal to the variable
- Creating instance of complex() class.
Creating a complex variable
- A complex variable can be created simply by assigning a
complex
value to a variable. - Code Sample :
c = 5 + 6j
print("c is a complex number.")
print(f"Value of c = {c}")
print(f"Type of c is {type(c)}")
# Output
# c is a complex number.
# Value of c = (5+6j)
# Type of c is <class 'complex'>
Creating an instance of complex class
- A complex variable can be created simply by creating instance of
complex
class. - Code Sample :
c = complex(5, 6)
print("c is a complex number.")
print(f"Value of c = {c}")
print(f"Type of c is {type(c)}")
# Output
# c is a complex number.
# Value of c = (5+6j)
# Type of c is <class 'complex'>
Prev. Tutorial : Float Data Type
Next Tutorial : Strings data type