An ad-free and cookie-free website.
This Python reference offers programmers a quick way to learn Python and also serves as a source for reminders.
While the version documented here is Python 3.5.3, most of this is suitable for other versions of Python 3. Check your version for details.
int data type represents integers
and is one of three basic numeric data types in Python.
Integers are whole numbers without a decimal point. They can be input with no sign, a positive sign, or a negative sign. There is no limit to the size of an integer except as limited by system memory.
Integers are immutable, which means they cannot be changed once constructed. To change an integer that has been assigned a name, you must reassign it.
int() function is a type
conversion function, so it can be used to convert floats and strings to
Here we assume the most basic import scenario without aliasing. See our reference document on importing modules for more.
Below is syntax for creating an integer object with assignment followed by examples.
Assignment will infer the data type as an integer if an integer literal is supplied. Integer literals can take the following forms.
Here we assign numbers in integer, octal, hexadecimal and binary forms. Notice how they are entered in numeric form as opposed to strings.
To change a string to an int assign a string in the format of an integer literal.
As you see in the last example, the leading zeros will be stripped from the string. Also, while the + sign is not required, Python will not throw an error.
To change a float to an int assign a floating point number in a similar fashion. As earlier, a string structured like a float works, but uninterpretable strings do not.
Notice how the fractional part right of the decimal drops off without
rounding, so it is truncated. To round first, use the built-in function
The built-in Python
int() function can
be called from programs and customized with parameters. A maximum of
2 arguments can be passed.
Pass the function an integer literal (see above).
When no arguments are passed, the default is 0.
Optionally enter a second parameter for a different base. The default is base 10. Here the object must be a string, bytes or bytesarray class.
A call to the
int() function without
assignment prints integer literals (described above) as an integer.
To access methods an assignment must be made to create an int object.
Syntax and examples below assume an integer object has been assigned to
Methods for the integer data type are provided below.
Returns the number of bits required to represent the provided integer.
Returns an array of bytes based on an integer.
Returns an integer based on an array of bytes.
Because the second two cover more advanced functionality see the official documentation for details.
Examples of integer data type methods.
To access local help on the Python int class type
help('int') at the Python Interpreter.
Output for Python 3.5.3 looks like this.
Subscribe to our growing YouTube Channel, a companion to this free online educational website. Follow @factorpad on Twitter for updates.
A newly-updated free resource. Connect and refer a friend today.