Python Long Int
What is an Integer in Python?
In Python, an int is a built-in numeric data type used to represent fixed-sized integers. It is used to store whole numbers without any fractional or decimal components. The maximum and minimum values that can be stored in an int depend on the platform Python runs. On most modern platforms, an int can represent values ranging from -231 to 231-1 or -263 to 263-1, depending on whether the platform is 32-bit or 64-bit, respectively.
Python supports all standard arithmetic operations on int, including addition, subtraction, multiplication, and division. In Python 3, the division operator / always returns a float, even if both operands are int, while the floor division operator // returns an int. The modulo operator % returns the remainder of the division of two int values.
Int and Long Int:
Python provides two built-in integer types: int and long int. While int can represent fixed-sized integers (usually 32 or 64 bits, depending on the platform), long int can represent arbitrarily large integers. The need for long int arises when we want to perform arithmetic operations on large integers that the int type cannot accommodate. Python automatically converts an int to a long int when the value of the int exceeds the maximum value that can be stored in an int. For example, on a 64-bit platform, the maximum value of an int is 2^63 - 1, or 9,223,372,036,854,775,807. If we try to assign a value greater than this to an int, Python will automatically promote it to a long int.
CODE:
a = 9223372036854775807 # Maximum value of int
print(type(a)) # <class 'int'>
b = 9223372036854775808 # One more than the maximum value of int
print(type(b)) # <class 'int'>
c = 9223372036854775808 + 1 # Now we are performing an arithmetic operation
print(type(c)) # <class 'long'>
OUTPUT:
<class 'int'>
<class 'int'>
<class 'int'>
CODE EXPLANATION:
This code declares three variables, a, b, and c, and assigns them the values of 9223372036854775807 and 9223372036854775808. The result of 9223372036854775808 + 1, respectively. ‘a’ and ‘b’ are both declared as integers, and since their values are within the range of what can be represented by an integer in Python, their types are both integers.
However, since the result of the arithmetic operation 9223372036854775808 + 1 is larger than the maximum value an int can represent, Python automatically promotes the result to a long data type. Therefore, the type of variable c is long. In Python, the long data type is not distinct from int but an int that can automatically be promoted to long when necessary to represent large integers.
Here's an example of using long int to calculate the factorial of a large number:
CODE:
n=int(input("Enter n value:"))
result = 1
for i in range(1, n+1):
result *= i
print(result)
OUTPUT:
Enter n value:120
6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000
CODE EXPLANATION:
This Python code calculates the factorial of a given number n. The user is prompted to enter a value for n using the input() function and is then converted to an integer using the int() function.
The code then initializes a variable called result to 1. The for loop iterates through the range of integers from 1 to n+1, with i taking on each value in that range. The loop body multiplies the current value of the result by the value of i using the *= operator. This is equivalent to result = result * i, accumulating the product of all the numbers from 1 to n in the result. Once the loop completes, the result contains the factorial of n, and the code prints this value using the print() function. For example, if the user enters five as the value of n, the program will calculate the factorial of 5 as 5 * 4 * 3 * 2 * 1 = 120 and output 120 to the console.
Python provides many built-in functions and modules that work with long int. For example, the math module provides functions for performing mathematical operations on long int, such as math.gcd() for calculating the greatest common divisor of two long int values. While long int is a powerful tool for working with large integers, it comes with a tradeoff in terms of performance. Because long int values require more memory to store than int values, arithmetic operations on long int values can be slower than operations on int values. If you are working with integers that can be represented by int, it is generally more efficient to use int instead of long int.
The Conclusion
In conclusion, a long int is a powerful tool for working with arbitrarily large integers in Python. It allows us to perform arithmetic operations on very large.