Python sympy.is_integer Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.is_integer Method:

We can use the sympy.is_ integer method to determine whether an element is an integer or not. This method returns a boolean value, True or False.

Syntax:

 sympy.is_ integer()

Return Value:

If the value is an integer, return True; otherwise, return False.

sympy.is_integer Method in Python

Method #1: Using is_integer Function (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number as static input and store it in a variable.
  • Pass the given number to the simplify() function and apply is_integer method on it to check whether the given number is an integer or Not.
  • Store it in another variable.
  • Print the result after checking if the given number is an integer or Not.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the number as static input and store it in a variable.
gvn_numb = 6

# Pass the given number to the simplify() function and apply is_integer method on it 
# to check whether the given number is an integer or Not
# Store it in another variable.
rslt = simplify(gvn_numb).is_integer

# Print the result after checking if the given number is an integer or Not
print("Checking if the given number{", gvn_numb,"} is an integer or Not = ", rslt)

Output:

Checking if the given number{ 6 } is an integer or Not = True

Method #2: Using is_integer Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number as user input using the input() function and store it in a variable.
  • Pass the given number to the simplify() function and apply is_integer method on it to check whether the given number is an integer or Not.
  • Store it in another variable.
  • Print the result after checking if the given number is an integer or Not.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Give the number as user input using the input() function and store it in a variable.
gvn_numb = input("Enter some random number = ")

# Pass the given number to the simplify() function and apply is_integer method on it 
# to check whether the given number is an integer or Not
# Store it in another variable.
rslt = simplify(gvn_numb).is_integer

# Print the result after checking if the given number is an integer or Not
print("Checking if the given number{", gvn_numb,"} is an integer or Not = ", rslt)

Output:

Enter some random number = 2.5
Checking if the given number{ 2.5 } is an integer or Not = False