Understanding Python Complex Literals
Python supports complex numbers, which are used in areas such as mathematics, electrical engineering, signal processing, and scientific computing. In Python, complex values can be written directly in code as complex literals or created during program execution through calculations and other operations.
In this guide, you’ll learn what Python complex literals are, their syntax, how they work, and how to use them with simple examples.
🚀 Getting Started: If you’re exploring literals for the first time, begin with our guide to understanding Python literals.
💡 Tip: To understand how Python stores and works with complex numbers internally, you can explore our guide on Python complex() function.
Introduction: What is a Complex Literal?
Definition: A Python complex literal is a numeric value written directly in Python code that consists of a real part and an imaginary part. The imaginary part is written using the letter j, which represents the imaginary unit.
Example: In the statement number = 3+4j, the value 3+4j is a complex literal because it contains both a real part (3) and an imaginary part (4j) written directly in the code.
Now that you know what a complex literal is, let’s understand how it is written in Python (Syntax).
How to Write Complex Literals in Python?
Python allows complex literals to be written directly in the source code without using any special function or constructor. A complex literal consists of a real part and an imaginary part, where the imaginary part is written using the letter j.
The syntax below illustrates the general structure of a Python complex literal.
Syntax
variable_name = <real_part> + <imaginary_part>jHere, variable_name is the name of the variable, real_part represents the real value (an integer or float), and imaginary_partj represents the imaginary value followed by j. Both parts can be positive, negative, or zero.
| Component | Description |
|---|---|
variable_name | The name of the variable used to store the complex literal. |
real_part | The real part of the complex number. It can be an integer or a float. |
imaginary_partj | The imaginary part followed by j. The value can be positive, negative, or zero. |
Python supports writing complex literals in different forms depending on how the real and imaginary parts are represented.
Let’s explore each form of Python complex literal in detail.
Forms of Python Complex Literals
Python complex literals can be written in different forms depending on the values of their real and imaginary parts:
- Complex Literal with Positive Real and Imaginary Parts
- Complex Literal with Negative Real and Imaginary Parts
- Complex Literal with Zero Imaginary Part
- Complex Literal with Zero Real Part
Let’s explore each form of Python complex literal with explanations and examples.
1. Complex Literal with Positive Real and Imaginary Parts
A standard complex literal contains both a real part and an imaginary part. The imaginary part is always followed by the letter j.
Example: Standard Complex Literal
complex_num = 3 + 4j
print(complex_num)
#Output
(3+4j)
Explanation: The value 3 + 4j is a complex literal. Here, 3 is the real part, and 4j is the imaginary part.
2. Complex Literal with Negative Real and Imaginary Parts
Both the real part and the imaginary part of a complex literal can be negative.
Example: Complex Literal with Negative Values
complex_num = -3 - 5j
print(complex_num)
# Output
(-3-5j)
Explanation: The value -3 - 5j is a complex literal in which both the real part and the imaginary part are negative.
3. Complex Literal with Zero Imaginary Part
A complex literal can also have an imaginary part equal to zero. Although it behaves like a real number in many situations, Python still treats it as a complex number.
Example: Complex Literal with Zero Imaginary Part
complex_num = 7 + 0j
print(complex_num)
# Output
(7+0j)
Explanation: The value 7 + 0j is a complex literal whose imaginary part is zero.
4. Complex Literal with Zero Real Part
When the real part is zero, the complex literal represents a pure imaginary value.
Example: Complex Literal with Zero Real Part
complex_num = 0 + 6j
print(complex_num)
# Output
6j
Explanation: The value 0 + 6j is a complex literal whose real part is zero and whose imaginary part is 6j.
Key Examples at a Glance: Complex Literals
The following table summarizes the different forms of Python complex literals:
| Type | Format | Example | Meaning |
|---|---|---|---|
| Standard Complex Literal | real + imaginaryj |
3 + 4j |
Contains both real and imaginary parts |
| Negative Complex Literal | -real – imaginaryj |
-3 - 5j |
Both parts are negative |
| Zero Imaginary Part | real + 0j |
7 + 0j |
Imaginary part is zero |
| Pure Imaginary Literal | 0 + imaginaryj |
0 + 6j |
Real part is zero |
Key Takeaways: Complex Literals
Here are the key points to remember about Python complex literals:
- A complex literal consists of a real part and an imaginary part.
- The imaginary part is always written using the letter
j. - Complex literals are written directly in Python code.
- The real part and the imaginary part can each be positive, negative, or zero.
- Complex literals are commonly used in mathematics, engineering, physics, and scientific computing.