How to Use complex() Function in PythonPython Basics


The complex() function returns a complex number with a real part and imaginary part.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

print("the value of complex(1, 2) is: ",complex(1, 2))

print("the value of complex(1, 0) is: ",complex(1, 0))

print("the value of complex(1, 3) is: ",complex(1, 3))

print("the value of complex(2, 2) is: ",complex(2, 2))

Output:

the value of complex(1, 2) is:  (1+2j)
the value of complex(1, 0) is:  (1+0j)
the value of complex(1, 3) is:  (1+3j)
the value of complex(2, 2) is:  (2+2j)

Syntax

complex(real, imaginary)

Parameters

Name Description
real A number representing the real part of the complex number.
imaginary A number representing the imaginary part of the complex number.

Return Value

It returns a complex number.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us