The Wayback Machine - https://web.archive.org/web/20240204145043/https://www.geeksforgeeks.org/sys-maxsize-in-python/
Open In App
Related Articles

sys.maxsize() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

maxsize attribute of the sys module fetches the largest value a variable of data type Py_ssize_t can store. It is the Python platform’s pointer that dictates the maximum size of lists and strings in Python. The size value returned by maxsize depends on the platform architecture:

  • 32-bit: the value will be 2^31 – 1, i.e. 2147483647
  • 64-bit: the value will be 2^63 – 1, i.e. 9223372036854775807

sys.maxsize

Syntax: sys.maxsize

Returns: maximum value of Py_ssize_t depending upon the architecture

Example 1: Let us fetch the maximum Py_ssize_t value on a 64-bit system.

Python3




# importing the module
import sys
       
# fetching the maximum value
max_val = sys.maxsize
print(max_val)


Output:

9223372036854775807

Example 2: Creating a list with the maximum size.

Python3




# importing the module
import sys
       
# fetching the maximum value
max_val = sys.maxsize
 
# creating list with max size
list = range(max_val)
 
# displaying the length of the list
print(len(list))
 
print("List successfully created")


Output

9223372036854775807
List successfully created

Output:

9223372036854775807
List successfully created

Example 3: Trying to create a list with a size greater than the maximum size.

Python3




# importing the module
import sys
       
# fetching the maximum value
max_val = sys.maxsize
 
try:
 
    # creating list with max size + 1
    list = range(max_val + 1)
 
    # displaying the length of the list
    print(len(list))
    print("List successfully created")
     
except Exception as e:
 
    # displaying the exception
    print(e)
    print("List creation unsuccessful")


Output

Python int too large to convert to C ssize_t
List creation unsuccessful

Output:

Python int too large to convert to C ssize_t
List creation unsuccessful

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 23 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials