sys.maxint в Python

Опубликовано: 12 Апреля, 2022

In programming, maxint/INT_MAX denotes the highest value that can be represented by an integer. In some cases, while programming, we may need to assign a value that is larger than any other integer value. Normally one assigns such values manually. For example, consider a list of integers where the minimum value has to be found out using a for loop.

Python

# initializing the list
li = [1, -22, 43, 89, 2, 6, 3, 16]
  
# assigning a larger value manually
curr_min = 999999
  
# loop to find minimum value
for i in range(0, len(li)):
  
    # update curr_min if a value lesser than it is found
    if li[i] < curr_min:
        curr_min = li[i]
  
print("The minimum value is " + str(curr_min))
Output
The minimum value is -22

В приведенном выше подходе мы предполагаем, что 999999 является максимально возможным значением в нашем списке, и сравниваем его с другими элементами для обновления, когда значение меньше найденного.

sys в Python

This module is used to interact with the interpreter and to access the variables maintained by the interpreter. It can be used to carry out manipulations in the runtime environment. This has to be imported like other packages to utilize the functionalities in it. Python’s sys module provides a variety of functions and constants among which the constant maxint, which can be used to set a positive integer value which is guaranteed to be larger than any other integer. Look at the example below.

Python

# import the module
import sys
  
# initializing the list
li = [1, -22, 43, 89, 2, 6, 3, 16]
  
# assigning a larger value with 
# maxint constant
curr_min = sys.maxint
  
# loop to find minimum value
for i in range(0, len(li)):
  
    # update curr_min if a value lesser 
    # than it is found
    if li[i] < curr_min:
        curr_min = li[i]
  
print("The minimum value is " + str(curr_min))
Output

The minimum value is -22