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

Loading editor…

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.

Loading editor…

2. Binary Number System (Base 2)

Binary numbers use only two digits:

  • 0
  • 1

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

Loading editor…

Invalid Binary Example

Loading editor…

3. Octal Number System (Base 8)

Octal numbers use digits from 0 to 7.

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

Loading editor…

Invalid Octal Example

Loading editor…

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.

Loading editor…

Invalid Hexadecimal Example

Loading editor…

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.

Loading editor…

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.

Loading editor…

2. oct() Function

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

Loading editor…

3. hex() Function

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

Loading editor…

Example Using Different Number Systems

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.