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.


int([number, [base]])

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

Optional. If used number must be a string.

Return Value:


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)
>>> int(0x10)
>>> int(0b10)

Example: 02

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

Example: 03

>>> int(3.14)
>>> int(3.14e10)
>>> int(-3.14)

Leave a Reply

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

You are commenting using your 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

Up ↑

%d bloggers like this: