Python Integer Data Type (int)
What is an Integer?
Integers are numbers without any decimal point.
# Examples
a = 10
b = 0
c = -25
All of these are integers because:
- They do not contain fractional values
- They represent whole numbers
- Integers are also called:
- Whole numbers
- Integral values
Integer Type in Python
Let us verify the type of an integer:
a = 10
print(type(a)) # <class 'int'>
This confirms that Python treats 10 as an object of type int.
a = 10
b = 123456789012345678901234567890
- In Python, no need to worry about size.
- Python automatically handles large numbers.
Python does not require separate data types like long in Python 3.
Ways to Represent Integers in Python
Python allows integers to be represented in four different number systems:
- Decimal
- Binary
- Octal
- Hexadecimal
1. Decimal Form (Base 10)
- Default number system
- Uses digits: 0–9
a = 123
b = 987
If no prefix is used, Python assumes the number is decimal.
2. Binary Form (Base 2)
Uses only 0 and 1.
a = 0b1111
print(a) # 15
Binary numbers must start with 0b or 0B.
3. Octal Form (Base 8)
Uses digits: 0–7
a = 0o123
print(a) # 83
8 and 9 are not allowed in octal. Octal numbers must start with 0o or 0O.
4. Hexadecimal Form (Base 16)
Uses digits:
- 0–9
- A–F (or a–f)
a = 0x10
print(a) # 16
Hexadecimal numbers must start with 0x or 0X.
Important Behavior of Python
Always Outputs in Decimal
No matter how you define the number:
a = 0b1010
print(a) # 10
Python always converts numbers to decimal when printing.
Base Conversion
If you want output in other formats:
bin()→ Binaryoct()→ Octalhex()→ Hexadecimal