Python Integer: Explained With Example Code

The Python integer is a non-fractional number, like 1, 2, 45, -1, -2, and -100. It’s one of the three types of numbers Python supports natively, the others being floating-point numbers and complex numbers.

Max size of a Python integer

Unlike many other programming languages, integers in Python 3 can have large values. In fact, they are unbounded, meaning there is no limit to their size, for example:

>>> num = 98762345098709872345000
>>> num + 1
98762345098709872345001

Of course, there is a limit, since your computer does not have unlimited memory. However, for all practical purposes, you don’t have to worry about it.

Integer types

Unlike Python 2 and many other languages, Python 3 has only one type of integer. This is part of Python’s aspiration to be a clean, easy to learn language. It’s one less thing we have to worry about. For more details, see PEP-0237.

Converting from and to an integer

String to integer

To convert a string to integer in Python, use the int() function:

>>> int('100')
100

Integer to string

To convert an integer to a string in Python, use the str() function:

>>> str(200)
'200'

Float to integer

To convert a float to an integer, use the int() function:

>>> int(2.3)
2

Python random integer

Many use cases require a random integer. For this, you need to import the module random. Be warned that this offers pseudo-randomness, which is not suitable for cryptography.

Let’s get a random number:

>>> import random
>>> random.randint(1,10)

The above instruction returns a pseudo-random number from 1 to 10 inclusive, which means including 1 and 10. For full details of the random module, visit the Python documentation.

Is it a Python integer?

To check if a value is an integer, we can use the type() function. It will return int for integers. Here’s a simple example of how to use this in an if-statement:

>>> type(2)
int
>>> if isinstance(2, int):
...     print('An integer')
... 
An integer

Don’t use if type(2) == int.
Using isinstance() is almost always the better, cleaner way and covers more use cases, like subclasses.

Python Courses

Are you enjoying this free tutorial? Please also have a look at my premium courses. They offer a superior user experience with small, easy-to-digest lessons and topics, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

The Python Fundamentals Course For Beginners
Now for $29 (from $49)

Python Fundamentals I is a course for beginners that will get you started with Python in no time. Learn all the essentials, test your progress with quizzes and assignments, and bring it all together with the final course project!

Python Course for Beginners

Modules, Packages, And Virtual Environments
Now for $29 (from $49)

Python Fundamentals II covers creating your own modules and packages, using virtual environments and Python package managers to make your life as a programmer easier. Advance your productivity as a Python programmer!

Python Fundamentals 2

Leave a Comment