Nearby lessons
15 of 108Python - int Data Type
Introduction
The int data type is used to store whole numbers (integers).
Integer values do not contain a decimal point.
Python supports both positive and negative integer values.
Example
main.py
10
Important Note
In Python 2, there were two integer types:
intlong
In Python 3, there is no separate long data type.
All integer values, including very large numbers, are stored using the int data type.
Ways to Represent Integer Values
Python allows integers to be written in four number systems.
- Decimal
- Binary
- Octal
- Hexadecimal
1. Decimal Number System (Base 10)
Decimal is the default number system in Python.
It uses digits from 0 to 9.
main.py
10
2. Binary Number System (Base 2)
Binary numbers use only two digits:
- 0
- 1
A binary number must start with 0b or 0B.
main.py
15
Invalid Binary Example
main.py
SyntaxError
3. Octal Number System (Base 8)
Octal numbers use digits from 0 to 7.
An octal number must start with 0o or 0O.
main.py
83
Invalid Octal Example
main.py
SyntaxError
4. Hexadecimal Number System (Base 16)
Hexadecimal numbers use:
- Digits: 0–9
- Letters: A–F or a–f
A hexadecimal number must start with 0x or 0X.
main.py
64206 48879
Invalid Hexadecimal Example
main.py
SyntaxError
How Python Stores Integer Values
Programmers can write integers in decimal, binary, octal, or hexadecimal form.
However, Python always stores and displays integer values in decimal form.
main.py
10 8 16 2
Base Conversion Functions
Python provides built-in functions to convert numbers between different number systems.
1. bin() Function
The bin() function converts a number into binary format.
main.py
0b1111 0b1001 0b10000
2. oct() Function
The oct() function converts a number into octal format.
main.py
0o12 0o17 0o443
3. hex() Function
The hex() function converts a number into hexadecimal format.
main.py
0x64 0x3f 0x14e5
Example Using Different Number Systems
main.py
10 10 10 10
Summary of Integer Number Systems
| Number System | Base | Prefix | Allowed Digits |
|---|---|---|---|
| Decimal | 10 | No Prefix | 0-9 |
| Binary | 2 | 0b | 0, 1 |
| Octal | 8 | 0o | 0-7 |
| Hexadecimal | 16 | 0x | 0-9, A-F, a-f |
Important Points
- The
intdata type is immutable. - Python integers can store very large values.
- Python automatically manages memory for integer objects.
- Python supports decimal, binary, octal, and hexadecimal numbers.
- Internally, integer values are displayed in decimal form.
Key Points
- The
intdata type stores whole numbers. - Python 3 uses only the
inttype for all integer values. - Binary numbers start with
0b. - Octal numbers start with
0o. - Hexadecimal numbers start with
0x. - Use
bin(),oct(), andhex()for base conversions.
Quick Summary
| Topic | Description |
|---|---|
| Data Type | int |
| Stores | Whole Numbers |
| Decimal Prefix | No Prefix |
| Binary Prefix | 0b |
| Octal Prefix | 0o |
| Hexadecimal Prefix | 0x |
| Conversion Functions | bin(), oct(), hex() |
| Mutable | No (Immutable) |