Python cmath.isfinite() Method with Examples

cmath.isfinite() Method in Python:

The cmath.isfinite() method determines whether or not a given complex value is finite.

This method gives the following Boolean value: If the value is finite, True; otherwise, False.

Syntax:

cmath.isfinite(x)

Parameters

x: This is Required. It is the value that is used to determine whether it is finite or not.

Return Value:

Returns a boolean value that is True if the value is finite and False otherwise.

Examples:

Example1:

Input:

Given Complex Number = float('inf')+ 7j

Output:

Check if the given complex number (inf+7j) is finite or not =  False

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

Check if the given complex number (5+2j) is finite or not = True

Note: The above input format is for dynamic input.

cmath.isfinite() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.isfinite() method that returns a boolean value that is True if the given complex number is finite and False otherwise.
  • Store it in another variable.
  • Print the above result after checking if the given complex number is finite or not.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.isfinite() method
# that returns a boolean value that is True if the given complex number is
# finite and False otherwise.
# Store it in another variable.
rslt = cmath.isfinite(complexnumb)
# Print the above result after checking if the given complex number is finite or not.
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Check if the given complex number (3+4j) is finite or not =  True

Similarly, try for the other examples

import cmath
complexnumb = complex(3, float('inf'))
rslt = cmath.isfinite(complexnumb)
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Check if the given complex number (3+infj) is finite or not =  False
import cmath
complexnumb = float('inf')+ 7j
rslt = cmath.isfinite(complexnumb)
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Check if the given complex number (inf+7j) is finite or not =  False

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.isfinite() method that returns a boolean value that is True if the given complex number is finite and False otherwise.
  • Store it in another variable.
  • Print the above result after checking if the given complex number is finite or not.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.isfinite() method
# that returns a boolean value that is True if the given complex number is
# finite and False otherwise.
# Store it in another variable.
rslt = cmath.isfinite(complexnumb)
# Print the above result after checking if the given complex number is finite or not.
print("Check if the given complex number",
      complexnumb, "is finite or not = ", rslt)

Output:

Enter real part and complex part of the complex number = 5 2
Check if the given complex number (5+2j) is finite or not = True