What Is an Integer in Python?
An integer in Python is a whole number. It does not have a decimal point.
Examples of integers:
-30742
In Python, the type name for an integer is int.
If you are new to Python, integers are one of the most common data types in Python. You will use them for counting, indexing, loops, and simple math.
Definition
An integer is:
- A whole number
- A number with no decimal point
- A value stored as the
inttype in Python
Examples:
a = 10
b = 0
c = -25
print(type(a))
print(type(b))
print(type(c))
Output:
<class 'int'>
<class 'int'>
<class 'int'>
What counts as an integer
These are integers:
- Positive whole numbers like
1,5, and100 - Negative whole numbers like
-1and-20 - Zero, which is
0
These are not integers:
3.50.0-2.75
Those values are decimal numbers, which Python usually stores as floats.
How Python shows integer type
You can check a value's type with the type() function.
print(type(5))
print(type(-8))
print(type(0))
Output:
<class 'int'>
<class 'int'>
<class 'int'>
This is useful when you are not sure what kind of value you have.
For example:
value = 12
print(value)
print(type(value))
Output:
12
<class 'int'>
If you want to learn more, see the Python type() function explained.
Common places integers are used
Beginners often use integers in these situations:
- Counting items
- Looping with
range() - Using list indexes
- Doing simple math
Example:
apples = 4
print(apples + 2)
for i in range(3):
print(i)
colors = ["red", "green", "blue"]
print(colors[1])
Output:
6
0
1
2
green
What is happening here:
apples = 4stores an integerrange(3)uses integers to control the loopcolors[1]uses the integer1as a list index
Integer example ideas
Here are some simple beginner examples.
Store age in a variable
age = 15
print(age)
print(type(age))
Output:
15
<class 'int'>
Count how many times a loop runs
for count in range(5):
print("Loop number:", count)
Output:
Loop number: 0
Loop number: 1
Loop number: 2
Loop number: 3
Loop number: 4
Access a list item by index
letters = ["a", "b", "c"]
print(letters[2])
Output:
c
Convert user input with int() when needed
The input() function returns text, not a number.
text = input("Enter your age: ")
age = int(text)
print(age)
print(type(age))
If the user enters 12, the output will be:
12
<class 'int'>
To learn more, see the Python input() function explained and Python int() function explained.
Integer vs float
An integer has no decimal part. A float can store decimal numbers.
Examples:
5is an integer5.0is a float3.14is a float
print(type(5))
print(type(5.0))
print(type(3.14))
Output:
<class 'int'>
<class 'float'>
<class 'float'>
Use the right type for the data you need:
- Use
intfor counting whole things - Use
floatfor values with decimals
For a bigger overview, see Python numbers explained: int, float, complex.
Related beginner confusion
A very common mistake is thinking that input() gives you an integer automatically. It does not. It gives you a string.
value = input("Enter a number: ")
print(value)
print(type(value))
If the user enters 12, the output will be:
12
<class 'str'>
To turn numeric text into an integer, use int():
value = input("Enter a number: ")
number = int(value)
print(number)
print(type(number))
If the user enters 12, the output will be:
12
<class 'int'>
But invalid text cannot be converted:
value = "hello"
number = int(value)
This causes an error because "hello" is not a valid integer. If you run into that problem, see ValueError: invalid literal for int() with base 10.
Common mistakes
Beginners often get confused about integers in these ways:
- Thinking numbers with decimals are integers
- Assuming
input()returns an integer automatically - Mixing strings and integers in calculations
- Using a float where Python expects an integer
- Confusing the
inttype with theint()conversion function
These quick checks can help:
print(value)
print(type(value))
print(isinstance(value, int))
What they do:
print(value)shows the current valueprint(type(value))shows its typeprint(isinstance(value, int))tells you whether it is an integer
Example:
value = 7
print(value)
print(type(value))
print(isinstance(value, int))
Output:
7
<class 'int'>
True
FAQ
Is 0 an integer in Python?
Yes. Zero is an integer.
Is 5.0 an integer?
No. 5.0 is a float because it has a decimal form.
How do I convert text to an integer in Python?
Use int(), such as:
number = int("5")
print(number)
What is the Python type for integers?
The type name is int.
Can integers be negative?
Yes. Values like -1 and -20 are integers.