Change language

# Complex Numbers in Python | Set 2 (Essential Functions and Constants)

|

1. exp () : — This function returns the exponent of the complex number mentioned in its argument.

2. log (x, b) : — This function returns the logarithmic base b value of x , both mentioned in their arguments. If no base is specified, the natural logarithm of x is returned.

 ` # Python code to demonstrate how it works ` ` # exp (), log () `   ` # import & quot; cmath & quot ; for operations with complex numbers ` ` import ` ` cmath ` ` import ` ` math `   ` # Initializing real numbers ` ` x ` ` = ` ` 1.0 ` ` y ` ` = ` ` 1.0 ` ` `  ` # convert x and y to complex number ` ` z ` ` = ` ` complex ` ` (x, y); `   ` # print complex number metric ` ` print ` ` (` ` "The exponent of complex number is:" ` `, end ` ` = ` ` "") ` ` print ` ` (cmath.exp (z)) ` ` `  ` # printing the complex issue form journal ` ` print ` ` (` ` "The log (base 10) of complex number is:" ` `, end ` ` = ` `" ") ` ` print ` ` (cmath.log (z, ` ` 10 ` `)) `

Exit d:

` The exponent of complex number is: (1.4686939399158851 + 2.2873552871788423j) The log (base 10) of complex number is: (0.15051499783199057 + 0.3410940884604603j) `

3. log10 () : — This function returns a base 10 logs of a complex number.

4.  sqrt () : — This calculates the square root of a complex number.

 ` # Python code to demonstrate how it works ` ` # log10 (), sqrt () ` ` # import & quot; cmath & quot; for operations with complex numbers ` ` import ` ` cmath ` ` import ` ` math `   ` # Initializing real numbers ` ` x ` ` = ` ` 1.0 ` ` y ` ` = ` ` 1.0 ` ` `  ` # convert x and y to complex number ` ` z ` ` = ` ` complex ` ` (x, y); `   ` # printing log10 complex number ` ` print ` ` (` ` "The log10 of complex number is:" ` `, end ` ` = ` ` "") ` ` print ` ` (cmath.log10 (z)) ` ` `  ` # printing the square root of the complex number ` ` print ` ` (` `" The square root of complex number is: "` `, end ` ` = ` ` "") ` ` print ` ` ( cmath. sqrt (z)) `

Output:

` The log10 of complex number is: (0.15051499 783199057 + 0.3410940884604603j) The square root of complex number is: (1.09868411346781 + 0.45508986056222733j) `

5. isfinite () : — Returns true if the real and imaginary parts of the complex number are finite , otherwise it returns false.

6. isinf () : — returns true if the real or imaginary part of the complex number is infinite , otherwise returns false.

7. isnan () : — returns true if the real or imaginary part of the complex number is NaN , otherwise returns false.

 ` # Python code to demonstrate how it works ` ` # isnan (), isinf (), isfinite () ` ` `  ` # import & quot; cmath & quot; for operations with complex numbers ` ` import ` ` cmath ` ` import ` ` math `   ` # Initializing real numbers ` ` x ` ` = ` ` 1.0 ` ` y ` ` = ` ` 1.0 ` ` a ` ` = ` ` math. inf ` ` b ` ` = ` ` math.nan `   ` # convert x and y to complex number ` ` z ` ` = ` ` complex ` ` (x, y); `   ` # convert x and a to a complex number ` ` w ` ` = ` ` complex ` ` (x, a); `   ` # convert x and b to complex number ` ` v ` ` = ` ` complex ` ` (x, b); `   ` # check if both numbers are finite ` ` if ` ` cmath.isfinite (z): ` ` print ` ` (` ` "Complex number is finite" ` `) ` ` else ` `: ` ` print ` ` (` `" Complex number is infinite "` `) `   ` # check if any number is infinite ` ` if ` ` cmath.isinf (w): ` ` print ` ` (` ` "Complex number is infinite" ` < code class = "plain">) ` else ` `: ` ` print ` ` (` `" Complex number is finite "` `) `   ` # check if any number is infinite ` ` if ` ` cmath.isnan (v): ` ` ` ` print ` ` (` ` "Complex number is NaN" ` `) ` ` else ` `: ` ` print ` ` (` `" Complex number is not NaN "` `) `

Output:

` Complex number is finite Complex number is infinite Complex numbe r is NaN `

Constants

The cmath module defines two constants “pi” that return a numeric value pi. The second —  "e", which returns the numeric value of the exponent.

 ` # Python code to demonstrate how it works ` ` # pi and e `   ` # import & quot; cmath & quot; for operations with complex numbers ` ` import ` ` cmath ` ` import ` ` math `   ` # printing pi value ` ` print ` ` (` ` "The value of pi is:" ` `, end ` ` = ` `" ") ` ` print ` ` (cmath.pi) `   ` # print e value ` ` print ` ` (` `" The value of exponent is: "` `, end ` ` = ` ` "") ` ` print ` ` (cmath.e) `` `

Output:

` The value of pi is: 3.141592653589793 The value of exponent is: 2.718281828459045 `

Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries