Python sympy.sets.Lopen() 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.sets.Lopen() Method:

Using the sympy.sets.Lopen() method, we can create a set of values by setting interval values such as left open, which indicates a set has a left open bracket and a right close bracket.

Syntax:

sympy.sets.Lopen(value_1, value_2)

Return Value:

A set of values with the left open set is returned by the Lopen() function.

sympy.sets.Lopen() Method in Python

Method #1: Using Lopen() Function (Static Input)

Approach:

  • Import Interval from sets of sympy module using the import keyword
  • Pass the lower and upper limits to the Lopen() function of the Interval of sympy module to get/open the set of values in the given range.
  • Here it includes the upper limit value while excluding the lower limit value.
  • Store it in a variable.
  • Print the above-obtained result set
  • Pass some random number to the contains() function to check whether the number passed exists in the above-obtained result set and print it.
  • The Exit of the Program.

Below is the implementation:

# Import Interval from sets of sympy module using the import keyword
from sympy.sets import Interval

# Pass the lower and upper limits to the Lopen() function of the Interval
# of sympy module to get/open the set of values in the given range.
# Here it includes the upper limit value(8) while excluding the lower limit value(1).
# Store it in a variable.
rslt_set = Interval.Lopen(1, 8)

# Print the above obtained result set
print("The above obtained result set = ", rslt_set)

# Pass some random number to the contains() function to check whether the
# number passed exists in the above obtained result set and print it.
print("Checking if 1 exists in the obtained result set:")
print(rslt_set.contains(1))

Output:

The above obtained result set = Interval.Lopen(1, 8)
Checking if 1 exists in the obtained result set:
False

Explanation:

Here, it opens the set containing the values from 1, 8 i,e the
set values does not include the lower limit value(1) but 
includes the upper limit value(8)

Method #2: Using Lopen() Function (User Input)

Approach:

  • Import Interval from sets of sympy module using the import keyword
  • Give the lower limit value as user input using the int(input()) function and store it in a variable.
  • Give the upper limit value as user input using the int(input()) function and store it in another variable.
  • Pass the above lower and upper limits as arguments to the Ropen() function of the Interval of sympy module to get/open the set of values in the given range.
  • Here it includes the lower limit value while excluding the upper limit value
  • Store it in a variable.
  • Print the above-obtained result set
  • Pass some random number to the contains() function to check whether the number passed exists in the above-obtained result set and print it.
  • The Exit of the Program.

Below is the implementation:

# Import Interval from sets of sympy module using the import keyword
from sympy.sets import Interval

# Give the lower limit value as user input using the int(input()) function 
# and store it in a variable.
lower_lmt = int(input("Enter some random number = "))

# Give the upper limit value as user input using the int(input()) function 
# and store it in another variable.
uppr_lmt = int(input("Enter some random number = "))

# Pass the above lower and upper limits as arguments to the the Lopen()function 
# of the Interval of sympy module to get/open the set of values in the given range.
# Here it includes the upper limit value while excluding the lower limit value.
# Store it in a variable.
rslt_set = Interval.Lopen(lower_lmt, uppr_lmt)

# Print the above obtained result set
print("The above obtained result set = ", rslt_set)

# Pass some random number to the contains() function to check whether the
# number passed exists in the above obtained result set and print it.
print("Checking if 5 exists in the obtained result set:")
print(rslt_set.contains(5))

Output:

Enter some random number = -5
Enter some random number = 5
The above obtained result set = Interval.Lopen(-5, 5)
Checking if 5 exists in the obtained result set:
True

Explanation:

Here, it opens the set containing the values from -5, 5 i,e the
set values does not include the lower limit value(-5) but includes the 
upper limit value(5)