Python complex() Function: Use, Syntax, and Examples

Python complex() function: In this tutorial, we will learn about the complex() function in Python with its use, syntax, parameters, returns type, and examples. By IncludeHelp Last updated : June 23, 2023

Python complex() function

The complex() function is a library function in Python, it is used to get the complex number from given a real number or/and an imaginary number (which is an optional part), it accepts either a real number only or read and an imaginary number and returns a complex number.

Syntax

The following is the syntax of complex() function:

complex(real [,Imaginary])

Parameter(s):

The following are the parameter(s):

  • real – value of a real number
  • Imaginary – an optional parameter, it's default value is 0.

Return Value

The return type of complex() function is <class 'complex'>, it returns a complex type of number containing the value in the form of (real + imaginary*j).

Python complex() Function: Example 1

In this program, we will create a complex number using the complex() function by providing the values of real and imaginary parameters.

# python code to demonstrate example 
# of complex() function

cnum = complex(10,2.2)
print("Complex number is:", cnum)

Output

Complex number is: (10+2.2j)

Python complex() Function: Example 2

In this program, we will create various complex numbers of the combinations of different values of real and imaginary parameters by using the complex() function.

# python code to demonstrate example 
# of complex() function

real = 10
img  = 20

# complex number  by passing real number
print(complex(real))

# complex number  by passing real & img. number
print(complex(real, img))

# complex number by passing real part as 0
print(complex(0))

# complex number by passing real and img. part as 0, 0
print(complex(0,0))

# with float values
real = 10.23
img  = 20.45

# complex number  by passing real number
print(complex(real))

# complex number  by passing real & img. number
print(complex(real, img))

Output

(10+0j)
(10+20j)
0j
0j
(10.23+0j)
(10.23+20.45j)



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.