Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python long() Function



The Pythonlong() function is used to represent integers of arbitrary size, allowing you to work with extremely large numbers that exceeded the limits of regular integers.

The long() function from Python 2 has been deprecated in Python 3. Instead, Python 3 uses the built-in int() function, which can handle arbitrarily large integers.

Syntax

Following is the syntax of Python long() function −

long(x [,base])

Parameters

This function takes two parameters as shown below −

  • x − It represents the value that you want to convert to a long integer.

  • base (optional) − It specifies the base of the number in 'x'. It can be any integer from 2 to 36. If not provided, the default base is "10".

Return Value

This function returns a long object.

Example 1

Following is an example of the Python long() function. Here, we have a large integer "number". We use the long() function to explicitly convert it into a long integer −

number = 123456789012345678901234567890long_number = long(number)print('The long value obtained is:',long_number)

Output

Following is the output of the above code −

('The long value obtained is:', 123456789012345678901234567890L)

Example 2

Here, we are converting a string representation of a large number into a long integer using the long() function −

string_number = "987654321098765432109876543210"long_number = long(string_number)print('The long value obtained is:',long_number)

Output

Output of the above code is as follows −

('The long value obtained is:', 987654321098765432109876543210L)

Example 3

Now, we are performing arithmetic operations with long integers, explicitly using the long() function for conversion −

num1 = 123456789012345678901234567890num2 = 987654321098765432109876543210result = long(num1) + long(num2)print('The addition of the long value obtained is:',result)

Output

The result obtained is as shown below −

('The addition of the long value obtained is:', 1111111110111111111011111111100L)

Example 3

In Python 2, we can use regular integers and long integers together in expressions. In this example, we combine (add) a regular integer with a long integer "num1" −

regular_integer = 42num1 = 123456789012345678901234567890combined_result = regular_integer + long(num1)print('The long value obtained is:',combined_result)

Output

We get the output as shown below −

('The long value obtained is:', 123456789012345678901234567932L)

Example 4

In the example below, the result of a large exponentiation operation automatically gets promoted to a long integer, displaying the automatic handling of long integers in Python 2 −

small_number = 12345result = small_number ** 20print('The long value obtained is:', result)

Output

The output produced is as follows −

('The long value obtained is:',  6758057543099832246538913025974955939211204840442478677426191001091098785400390625L)
python_type_casting.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp