Nearby lessons

15 of 159

Python - int Data Type

📌 What You Will Learn
  • 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

🐍Code Cell
1a = 10
2 
3print(a)
4print(type(a))
Output
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.

🐍Code Cell
1a = 10
2 
3print(a)
Output
10

2. Binary Number System (Base 2)

Binary numbers use only two digits:

  • 0
  • 1

A binary number must start with 0b or 0B.

🐍Code Cell
1a = 0B1111
2 
3print(a)
Output
15

Invalid Binary Example

🐍Code Cell
1a = 0B123
Output
SyntaxError

3. Octal Number System (Base 8)

Octal numbers use digits from 0 to 7.

An octal number must start with 0o or 0O.

🐍Code Cell
1a = 0o123
2 
3print(a)
Output
83

Invalid Octal Example

🐍Code Cell
1a = 0o786
Output
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.

🐍Code Cell
1a = 0XFACE
2b = 0XBeef
3 
4print(a)
5print(b)
Output
64206
48879

Invalid Hexadecimal Example

🐍Code Cell
1a = 0XBeer
Output
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.

🐍Code Cell
1a = 10
2b = 0o10
3c = 0X10
4d = 0B10
5 
6print(a)
7print(b)
8print(c)
9print(d)
Output
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.

🐍Code Cell
1print(bin(15))
2print(bin(0o11))
3print(bin(0X10))
Output
0b1111
0b1001
0b10000

2. oct() Function

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

🐍Code Cell
1print(oct(10))
2print(oct(0B1111))
3print(oct(0X123))
Output
0o12
0o17
0o443

3. hex() Function

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

🐍Code Cell
1print(hex(100))
2print(hex(0B111111))
3print(hex(0o12345))
Output
0x64
0x3f
0x14e5

Example Using Different Number Systems

🐍Code Cell
1a = 10
2b = 0B1010
3c = 0O12
4d = 0X0A
5 
6print(a)
7print(b)
8print(c)
9print(d)
Output
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)
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8