# Complex Numbers in Python

In this Python Tutorial, we will learn about Complex Numbers in Python. In addition, we will learn how to input complex numbers in Python with different examples.

## Introduction to Complex Numbers

Before delving into the Python aspect, let’s take a moment to understand what complex numbers are.

A complex number is a number that can be expressed in the form a + bi, where ‘a’ and ‘b’ are real numbers, and ‘i’ is the imaginary unit with the property i² = -1. ‘a’ is referred to as the real part, while ‘b’ is the imaginary part of the complex number.

## Creating Complex Numbers in Python

Python provides two ways to define a complex number:

1.The first method involves directly assigning the real and imaginary parts with the `j` notation:

``````# Defining a complex number
z = 3 + 4j
print(z) ``````

Output:

2.The second method involves using the built-in `complex()` function, which accepts the real and imaginary parts as arguments and returns a complex number:

``````# Using the complex() function to define a complex number
z = complex(3, 4)
print(z)``````

Output:

## Accessing Attributes of Complex Numbers in Python

Python provides two attributes, `real` and `imag`, for a complex number object, which returns the real and imaginary parts of the complex number, respectively.

Here is an example:

``````# Creating a complex number
z = 3 + 4j

# Printing the real and imaginary parts
print(z.real)
print(z.imag)
``````

Output:

## Input Complex Number in Python

You can input a complex number in Python by using the `input()` function and then converting the input to a complex number using the `complex()` function.

Here’s a simple Python program that prompts the user to input a complex number:

``````# Program to input a complex number

# Prompt user for real part of the complex number
real_part = float(input("Enter the real part: "))

# Prompt user for imaginary part of the complex number
imaginary_part = float(input("Enter the imaginary part: "))

# Form the complex number
z = complex(real_part, imaginary_part)

# Print the complex number
print("The complex number is: ", z)
``````

This program prompts the user to enter the real and imaginary parts separately. The `input()` function returns a string, so we use the `float()` function to convert the inputs to floating-point numbers. The `complex()` function then takes these two numbers and forms a complex number.

Output:

You can also get the complex number as a string and then convert it to a complex number. Here’s how:

``````# Program to input a complex number as a string

# Prompt user for the complex number as a string
z_string = input("Enter a complex number in the form a+bj: ")

# Convert the string to a complex number
z = complex(z_string)

# Print the complex number
print("The complex number is: ", z)
``````

In this program, the user must input the complex number in the form `a+bj` or `a-bj`, where `a` is the real part and `b` is the imaginary part. Again, you should add error handling to make this program robust.

Output:

## Conclusion

Python natively supports complex numbers, which are crucial for many mathematical computations. Complex numbers can be defined directly using `a + bj` syntax or using the `complex()` function. Python allows access to real and imaginary parts through the `.real` and `.imag` attributes respectively.

You may also like to read the following Python tutorials.