Nearby lessons
15 of 159Python - int Data Type
- Define the int data type and the values it stores
- Write integer values in decimal, binary, octal, and hexadecimal form
- Identify the prefixes used for binary, octal, and hexadecimal literals
- Convert numbers between bases using bin(), oct(), and hex()
- Explain why Python 3 uses only the int type for all integers
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
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.
2. Binary Number System (Base 2)
Binary numbers use only two digits:
- 0
- 1
A binary number must start with 0b or 0B.
Invalid Binary Example
3. Octal Number System (Base 8)
Octal numbers use digits from 0 to 7.
An octal number must start with 0o or 0O.
Invalid Octal Example
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.
Invalid Hexadecimal Example
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.
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.
2. oct() Function
The oct() function converts a number into octal format.
3. hex() Function
The hex() function converts a number into hexadecimal format.
Example Using Different Number Systems
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) |
- The int data type stores whole numbers
- Binary literals start with 0b, octal literals with 0o, and hexadecimal literals with 0x
- Python always stores and displays integer values in decimal form
- Python 3 uses int for all integer values, including very large numbers
- bin(), oct(), and hex() convert a number to binary, octal, and hexadecimal format