Nearby lessons

15 of 108

Python - 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
Python 3 Runtime
Loading Editor...
Output Preview
10

Important Note

In Python 2, there were two integer types:

  • int
  • long

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.

  1. Decimal
  2. Binary
  3. Octal
  4. Hexadecimal

1. Decimal Number System (Base 10)

Decimal is the default number system in Python.

It uses digits from 0 to 9.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
15

Invalid Binary Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
83

Invalid Octal Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
64206
48879

Invalid Hexadecimal Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
0b1111
0b1001
0b10000

2. oct() Function

The oct() function converts a number into octal format.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0o12
0o17
0o443

3. hex() Function

The hex() function converts a number into hexadecimal format.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0x64
0x3f
0x14e5

Example Using Different Number Systems

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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 int data 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 int data type stores whole numbers.
  • Python 3 uses only the int type for all integer values.
  • Binary numbers start with 0b.
  • Octal numbers start with 0o.
  • Hexadecimal numbers start with 0x.
  • Use bin(), oct(), and hex() for base conversions.

Quick Summary

Topic Description
Data Typeint
StoresWhole Numbers
Decimal PrefixNo Prefix
Binary Prefix0b
Octal Prefix0o
Hexadecimal Prefix0x
Conversion Functionsbin(), oct(), hex()
MutableNo (Immutable)

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - float Data Type