杰瑞科技汇

python int to long

Of course! This is a great question because the answer has changed significantly between Python 2 and Python 3.

python int to long-图1
(图片来源网络,侵删)

Here’s a complete breakdown.

The Short Answer (For Python 3)

In Python 3, there is no need to convert an int to a long. The int type is arbitrary-precision, meaning it can grow to accommodate any integer size, limited only by the available memory.

The old long type from Python 2 has been removed entirely. All integers are now ints.

Example:

# In Python 3, this is just an int. It can be huge.
big_number = 123456789012345678901234567890
print(type(big_number))
# You can perform operations on it without any issues.
even_bigger_number = big_number * 1000000
print(even_bigger_number)
print(type(even_bigger_number))

Output:

<class 'int'>
1234567890123456789012345678900000000
<class 'int'>

As you can see, Python automatically handles the large number, and its type remains int.


The Detailed Answer (Python 2 vs. Python 3)

The confusion usually comes from people who have used Python 2 or are reading older documentation.

Python 2: int vs. long

In Python 2, there were two distinct integer types:

  1. int: A fixed-size integer. It was typically 32 or 64 bits, depending on the system. If a number was too large to fit in this space, it would "wrap around" or cause an OverflowError.
  2. long: An arbitrary-precision integer. It was created by adding an L to the end of a number (e.g., 10000000000L). It could grow as large as memory allowed.

The Problem in Python 2: If you performed an operation that resulted in a number too large for an int, Python would sometimes silently "promote" it to a long, but this could lead to unexpected behavior or errors if your code wasn't designed to handle it.

The Conversion in Python 2: If you had an int and you knew it might become too large, or if you needed to ensure it was a long, you used the built-in long() function.

# Python 2 Example
# This is a regular int
my_int = 100
print(type(my_int))  # <type 'int'>
# Convert the int to a long
my_long = long(my_int)
print(type(my_long))  # <type 'long'>
# A number that is too big for a 32-bit int is automatically a long
big_num = 12345678901234567890
print(type(big_num))  # <type 'long'>

Summary Table

Feature Python 2 Python 3
Integer Types int (fixed-size) and long (arbitrary-precision) Only int (arbitrary-precision)
Automatic Promotion Yes, but could be unpredictable. Yes, seamless and automatic.
Conversion Function long(my_int) to convert an int to a long. Does not exist. The long type is gone.
How to denote a long 100L or 10000000000000000000 (without L) Just write the number: 100 or 10000000000000000000
Overflow Possible with int, causing errors or silent promotion. Not possible. The int type grows as needed.

Practical Advice for Modern Python (3.x)

If you are writing code today, you should:

  1. Forget about long. It does not exist.
  2. Simply use int for all your integer needs.
  3. Trust that Python will handle numbers of any size for you.
# This is the modern, Python 3 way to work with large integers.
# No conversion is ever needed.
# A very large number
factorial_of_100 = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
print(f"The factorial of 100 has {len(str(factorial_of_100))} digits.")
print(type(factorial_of_100))

Output:

The factorial of 100 has 158 digits.
<class 'int'>

What if you encounter long in old code?

If you are maintaining legacy Python 2 code that needs to run on Python 3, you might see long(). The best practice is to remove it. The long() function in Python 3 is an alias for int() for backward compatibility. So, long(x) in Python 2 is equivalent to int(x) in Python 3.

Migration Example:

Python 2 Code:

# This code ensures the number is a long
def calculate_large_value(start):
    result = long(start)
    for i in range(1000):
        result *= i
    return result

Equivalent Python 3 Code:

# The 'long()' call is redundant and can be removed.
def calculate_large_value(start):
    result = int(start) # or just start
    for i in range(1000):
        result *= i
    return result
# Even simpler, since the input is already an int:
def calculate_large_value_v2(start):
    result = start
    for i in range(1000):
        result *= i
    return result
分享:
扫描分享到社交APP
上一篇
下一篇