Python Integer Numbers

These represent numbers in the range -2147483648 through 2147483647. When the result of an operation would fall outside this range, the result is normally returned as a long integer (in some cases, the exception OverflowError is raised instead).

Plain integers (also just called integers) are implemented using long in C, which gives them at least 32 bits of precision (sys.maxint is always set to the maximum plain integer value for the current platform; the minimum value is -sys.maxint – 1). Long integers have unlimited precision.

int( )

Returns an expression converted into an integer number.

Syntax:


int([number, [base]])

Number:
Optional. Can be of string, int, float or long type.

Base:
Optional. If used number must be a string.

Return Value:

int

If the number is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

If the number is not a number or if the base is given, then the number must be a string or Unicode object representing an integer literal. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

Example: 01


>>>#this example converts octal, hex and binary integers into a decimal integer
>>> int(0o10)
8
>>> int(0x10)
16
>>> int(0b10)
2

Example: 02


>>> # this example uses base argument
>>> int('0101', 2)
5
>>> int('0101', 8)
65
>>> int('0101', 16)
257
>>> int('0101', 10)
101

Example: 03


>>> int(3.14)
3
>>> int(3.14e10)
31400000000L
>>> int(-3.14)
-3
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: