geekyjazzy
0
Q:

max int python

import sys
MAX_INT = sys.maxsize
print(MAX_INT)

''' NOTE: value of sys.maxsize is depend on the fact that how much bit a machine is. '''
0
>>> L=[2,-7,3,3,6,2,5]
>>> max(L)
6
3
import sys
MAX_INT = sys.maxsize - 1
0
# find largest item in the string
print(max("abcDEF")) 

# find largest item in the list
print(max([2, 1, 4, 3])) 

# find largest item in the tuple
print(max(("one", "two", "three"))) 
'two'

# find largest item in the dict
print(max({1: "one", 2: "two", 3: "three"})) 
3

# empty iterable causes ValueError
# print(max([])) 

# supressing the error with default value
print(max([], default=0)) 
1
What is the maximum possible value of an integer in Python ?
Consider below Python program.

  # A Python program to demonstrate that we can store 
  # large numbers in Python 

  x = 10000000000000000000000000000000000000000000; 
  x = x + 1
  print (x) 
  Output :

  10000000000000000000000000000000000000000001
  
In Python, value of an integer is not restricted by the number of bits and can expand to the limit of the available memory (Sources : this and this). Thus we never need any special arrangement for storing large numbers (Imagine doing above arithmetic in C/C++).

As a side note, in Python 3, there is only one type “int” for all type of integers. In Python 2.7. there are two separate types “int” (which is 32 bit) and “long int” that is same as “int” of Python 3.x, i.e., can store arbitrarily large numbers.

  # A Python program to show that there are two types in 
  # Python 2.7 : int and long int 
  # And in Python 3 there is only one type : int 

  x = 10
  print(type(x)) 

  x = 10000000000000000000000000000000000000000000
  print(type(x)) 

  Output in Python 2.7 :

  <type 'int'>
  <type 'long'>
  Output in Python 3 :

  <type 'int'>
  <type 'int'>
  
We may want to try more interesting programs like below :

  # Printing 100 raise to power 100 
  print(100**100) 
0
print(max(2, 3)) # Returns 3 as 3 is the largest of the two values
print(max(2, 3, 23)) # Returns 23 as 23 is the largest of all the values

list1 = [1, 2, 4, 5, 54]
print(max(list1)) # Returns 54 as 54 is the largest value in the list
0

New to Communities?

Join the community