Google News
logo
Python Program to Create Complex Numbers
In the following example of Python program that demonstrates how to create complex numbers :
Program :
# Creating complex numbers
num1 = 2 + 3j
num2 = -4j
num3 = complex(5, -2)

# Outputting the complex numbers
print(num1)
print(num2)
print(num3)
Output :
(2+3j)
(-0-4j)
(5-2j)
In this program, we create three complex numbers using the `j` notation to indicate the imaginary part of the number. The first number, `num1`, is equal to `2 + 3j`, the second number, `num2`, is equal to `-4j`, and the third number, `num3`, is equal to `5 - 2j`. Alternatively, we can also create a complex number using the `complex()` constructor function, as shown with `num3`.

We then use the `print` function to output the values of the complex numbers to the console.

You can perform various arithmetic operations on complex numbers in Python, such as addition, subtraction, multiplication, and division. You can also access the real and imaginary parts of a complex number using the `real` and `imag` attributes, respectively. For example:
Program :
num = 2 + 3j
print(num.real)   # 2.0
print(num.imag)   # 3.0