Lesson 01
int — Integer Data Type
Store whole numbers with int, inspect their type and identity, and work with decimal, binary, octal, and hexadecimal literals in Python.
Visual reference
Use these diagrams alongside the lesson text and the number-system SVG below.
What you will learn
- Where
intfits among Python’s 14 built-in data types - How
intstores whole numbers (no decimal point) - Using
type()andid()to inspect values - Decimal, binary, octal, and hexadecimal literals and
bin(),oct(),hex()
Big picture: 14 Python data types
Data types tell Python how much memory to reserve and how to interpret stored values. Your notes list 14 types in 6 categories:
| Category | Types |
|---|---|
| Fundamental | int, float, bool, complex |
| Sequence | str, bytes, bytearray, range |
| List (collection) | list, tuple |
| Set (collection) | set, frozenset |
| Dict (collection) | dict |
| NoneType | None (NoneType) |
This lesson is #1 in that index: int.
What is int?
int is a pre-defined class and a fundamental data type.
Purpose: store integer data — integral values, whole numbers without a decimal point — such as serial numbers, account numbers, employee IDs, and counts.
Valid int values |
Not integers |
|---|---|
0, 7, 100, -42 |
3.14, 10.5 |
Analogy
Think of int as a locker that accepts only whole items: you can store 10 apples, not 10.5 apples.
Inspecting an integer
Use type() to see the data type and id() to see the object’s identity in memory.
a = 100
print(a)
print(type(a))
print(id(a))Expected output (your id() number will differ each run):
100
<class 'int'>
140718856480280
id()changes every time the program runs. That is normal.
Arithmetic with integers
When both operands are integers, the result is also an int.
a = 10
b = 20
c = a + b
print(c)
print(type(c))30
<class 'int'>The four number systems
By default you write decimal (base 10). Python also lets you write binary, octal, and hexadecimal literals. They all become int values and usually print in decimal.
| System | Base | Prefix | Example literal |
|---|---|---|---|
| Decimal | 10 | (none) | 25 |
| Binary | 2 | 0b / 0B |
0b101 |
| Octal | 8 | 0o / 0O |
0o16 |
| Hexadecimal | 16 | 0x / 0X |
0xAC |
Visual: same value, four notations
The value 172 can be written in four ways:
All four represent the same integer: 172.
Binary
Binary uses only 0 and 1. Each digit is a bit. OS and processors work with binary during execution.
a = 0b101
print(a, type(a))
print(bin(5))5 <class 'int'>
0b101Invalid digit:
# a = 0b10102
# SyntaxError: invalid digit '2' in binary literalOctal
Octal uses digits 0–7 (base 8).
a = 0o16
print(a)
print(oct(14))14
0o16# a = 0o168
# SyntaxError: invalid digit '8' in octal literalHexadecimal
Hex uses 0–9 and A–F (or a–f), base 16 — common in OS and low-level work.
a = 0xAC
print(a)
print(hex(172))172
0xaca = 0xFACE
print(a) # 64206Conversion helpers
| Function | Returns |
|---|---|
bin(x) |
Binary string, e.g. '0b101' |
oct(x) |
Octal string, e.g. '0o16' |
hex(x) |
Hex string, e.g. '0xac' |
x can be a decimal, binary, octal, or hex integer — Python normalizes to int first.
n = 255
print(bin(n))
print(oct(n))
print(hex(n))0b11111111
0o377
0xffInteractive conversion (playground)
There is no separate “type a number” widget in the lesson body — use the Python playground at the bottom of this page. The starter code prints bin(), oct(), and hex() for 255. Change n to any whole number and run again to see binary, octal, and hexadecimal instantly.
Try it in the playground
Scroll to Try it in the playground, run the starter code, then:
- Store your age or roll number; print
type()of it. - Convert
255withbin(),oct(), andhex(). - Predict the output of
print(0b1111)before you run it. (Answer:15.)
Practice exercise 1 — your age
age = 25
print(age)
print(type(age))Practice exercise 2 — convert 255
n = 255
print(bin(n))
print(oct(n))
print(hex(n))Practice exercise 3 — predict output
print(0b1111)Key terms
| Term | Meaning |
|---|---|
| Literal | A value written in code, e.g. 100 or 0xAC |
| Base | Digits per place: 2, 8, 10, or 16 |
| Whole number | Integer with no fractional part |
| Prefix | 0b, 0o, 0x mark binary, octal, hex literals |
Quick knowledge check
-
Which prefix starts a binary literal?
0bor0B. -
What does
print(0o16)display?
14(octal16is decimal 14). -
Which function converts an
intto a hexadecimal string?
hex(). -
Can
3.14be stored in anintwithout conversion?
No — usefloator convert explicitly.
Summary
intstores whole numbers; use it for IDs, counts, and integer math.type()andid()help you inspect values while learning.- Literals: decimal (default),
0b,0o,0x— all areint. bin(),oct(), andhex()show other base representations as strings.
Next lesson: float — real numbers, scientific notation, and decimal-only literals.
Loading playground…