How To Write Python Complex Number With Examples

In Python, a complex number is a number with a real and imaginary part represented by `x + yj`, where `x` is the real part, `y` is the imaginary part, and `j` is the imaginary unit (which satisfies the equation `j^2 = -1`). In this article, I will show you how to create complex number in python with examples.

1. How To Create Complex Number In Python.

  1. You can create a complex number in Python by using the `complex()` function or by directly writing the number in the form `x + yj`.
  2. Here are some examples.

1.1 Creating complex numbers using complex().

  1. Blow example will create 2 complex numbers using the python complex() function.
    >>> a = complex(2, 3)
    >>> b = complex(1, -4)
    >>> print(a)
    >>> print(b)

1.2 Creating complex numbers directly.

  1. You can also use the below python source code to create complex numbers directly.
    d = -3.5 + 0j
    # Below is the output.

1.3 Perform arithmetic operations with complex numbers.

  1. You can perform arithmetic operations with complex numbers in the same way as you do with real numbers.
    a = 2 + 3j
    b = 1 - 4j
    print(a + b)
    print(a * b)
    print(a / b)
    Below is the above source code output.

1.4 How to access the real and imaginary parts of a complex number.

  1. In addition, you can access the real and imaginary parts of a complex number using the `real` and `imag` attributes respectively.
    a = 2 + 3j
    #Below is the above code output.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.