In Python, the complex() function is a built-in function that creates a complex number. A complex number is a number that consists of a real part and an imaginary part, both of which are floating-point numbers. The complex() function takes two arguments: the real part and the imaginary part of the complex number. In this article, we'll explore the complex() function in more detail and provide some examples of how it can be used.
Syntax:
The syntax for the complex() function is as follows:
|
complex([real[, imag]])
|
Here, the real parameter is optional and specifies the real part of the complex number. If it is not specified, it defaults to 0. The imag parameter is also optional and specifies the imaginary part of the complex number. If it is not specified, it defaults to 0.
Usage:
The complex() function is typically used to create complex numbers for use in mathematical operations. Complex numbers are often used in scientific and engineering applications, such as signal processing and control systems. They are also used in graphics and game programming to represent 2D and 3D points.
Examples:
Let's take a look at some examples of how the complex() function can be used in Python:
Example 1: Creating a complex number with default values
|
c = complex()
print(c)
|
In this example, we create a complex number c using the complex() function with no arguments. Since no arguments are provided, the real and imaginary parts of the complex number default to 0. The output of the program is (0+0j).
Example 2: Creating a complex number with specified real and imaginary parts
|
c = complex(2, 3)
print(c)
|
In this example, we create a complex number c using the complex() function with two arguments: 2 and 3. These values are used as the real and imaginary parts of the complex number, respectively. The output of the program is (2+3j).
Example 3: Creating a complex number from a string
|
c = complex('2+3j')
print(c)
|
In this example, we create a complex number c using the complex() function with a single string argument: '2+3j'. The string represents a complex number with a real part of 2 and an imaginary part of 3. The complex() function parses the string and creates a complex number with those values. The output of the program is (2+3j).
Example 4: Converting a floating-point number to a complex number
|
x = 2.5
c = complex(x)
print(c)
|
In this example, we create a floating-point number x with the value 2.5. We then create a complex number c using the complex() function with x as the real part and 0 as the imaginary part. This effectively converts the floating-point number x to a complex number. The output of the program is (2.5+0j).
Example 5: Converting an integer to a complex number
|
x = 2
c = complex(x)
print(c)
|
In this example, we create an integer x with the value 2. We then create a complex number c using the complex() function with x as the real part and 0 as the imaginary part. This effectively converts the integer x to a complex number. The output of the program is (2+0j).