Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner20 min

Visual reference

Use these diagrams alongside the lesson text and the number-system SVG below.

Python Data Types and numbers overviewPython Data Types and numbers overview
Number systems in maths — decimal, binary, octal, and hexadecimalNumber systems in maths — decimal, binary, octal, and hexadecimal
Numeric data types in PythonNumeric data types in Python

What you will learn

  • Where int fits among Python’s 14 built-in data types
  • How int stores whole numbers (no decimal point)
  • Using type() and id() 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.

python
a = 100

print(a)
print(type(a))
print(id(a))

Expected output (your id() number will differ each run):

text
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.

python
a = 10
b = 20
c = a + b

print(c)
print(type(c))
text
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:

Decimal, binary, octal, and hex representations of 172

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.

python
a = 0b101
print(a, type(a))
print(bin(5))
text
5 <class 'int'>
0b101

Invalid digit:

python
# a = 0b10102
# SyntaxError: invalid digit '2' in binary literal

Octal

Octal uses digits 0–7 (base 8).

python
a = 0o16
print(a)
print(oct(14))
text
14
0o16
python
# a = 0o168
# SyntaxError: invalid digit '8' in octal literal

Hexadecimal

Hex uses 0–9 and A–F (or a–f), base 16 — common in OS and low-level work.

python
a = 0xAC
print(a)
print(hex(172))
text
172
0xac
python
a = 0xFACE
print(a)  # 64206

Conversion 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.

python
n = 255
print(bin(n))
print(oct(n))
print(hex(n))
text
0b11111111
0o377
0xff

Interactive 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:

  1. Store your age or roll number; print type() of it.
  2. Convert 255 with bin(), oct(), and hex().
  3. Predict the output of print(0b1111) before you run it. (Answer: 15.)

Practice exercise 1 — your age

python
age = 25
print(age)
print(type(age))

Practice exercise 2 — convert 255

python
n = 255
print(bin(n))
print(oct(n))
print(hex(n))

Practice exercise 3 — predict output

python
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

  1. Which prefix starts a binary literal?
    0b or 0B.

  2. What does print(0o16) display?
    14 (octal 16 is decimal 14).

  3. Which function converts an int to a hexadecimal string?
    hex().

  4. Can 3.14 be stored in an int without conversion?
    No — use float or convert explicitly.

Summary

  • int stores whole numbers; use it for IDs, counts, and integer math.
  • type() and id() help you inspect values while learning.
  • Literals: decimal (default), 0b, 0o, 0x — all are int.
  • bin(), oct(), and hex() show other base representations as strings.

Next lesson: float — real numbers, scientific notation, and decimal-only literals.

Loading playground…