Skip to content

Module 3 · Variables and Data Types — topic 5 of 15

Integers

The int type for whole numbers: writing integers, unlimited size, underscores for readability, other number bases, and the operations and functions you use with them.

Beginner 12 min read·Lesson 33 of 306 in Python Programming

Learning objectives

  • Write integer literals including large numbers
  • Use binary, octal and hexadecimal forms
  • Apply common integer functions such as abs, divmod and pow

Definition

The int type for whole numbers: writing integers, unlimited size, underscores for readability, other number bases, and the operations and functions you use with them.

Introduction

Integers are whole numbers without a fractional part: -3, 0, 42, 1_000_000. Python’s int type represents them exactly, no matter how big they get.

Why it matters

Counts, indexes, ages, quantities, years and money in paise are all integers. Because Python integers are exact and unlimited, you never have to worry about overflow errors that affect many other languages.

Detailed explanation

Integer literals are written with digits and an optional minus sign. Leading zeros are not allowed in decimal integers (007 is a SyntaxError).

Underscores can separate digit groups for readability: 1_00_000 and 100_000 are both one hundred thousand. Python ignores the underscores.

Python ints have arbitrary precision: they grow to use as much memory as needed, so 2 ** 200 is calculated exactly.

Other bases: 0b1010 (binary), 0o17 (octal) and 0xFF (hexadecimal) are also integers. bin(), oct() and hex() convert an int to those text forms, and int("ff", 16) converts back.

Useful built-ins: abs(-5) is 5; divmod(17, 5) returns (3, 2), the quotient and remainder together; pow(2, 10) is 1024.

Integer arithmetic with +, -, * and // stays an int; / always gives a float.

Converting text to an int is strict: int(" 42 ") works because surrounding spaces are ignored, but int("4 2") and int("42.0") fail. When reading numbers that people type, decide whether you need a whole number and validate before converting.

Syntax

Python
count = 42
population = 1_400_000_000
flags = 0b1011
colour = 0xFF8800
  • Plain digits for ordinary integers.
  • Underscores group digits for readability.
  • 0b, 0o and 0x prefixes write binary, octal and hexadecimal integers.

Code examples

Exact and unlimited

Python
print(2 ** 100)
print(type(2 ** 100).__name__)
Output
1267650600228229401496703205376
int

Readable literals and bases

Python
print(1_00_000 + 5)
print(0b1010, 0o17, 0xFF)
print(bin(10), hex(255))
Output
100005
10 15 255
0b1010 0xff

More examples

Splitting seconds with divmod

Python
total_seconds = 9_245
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
print(f"{hours}h {minutes}m {seconds}s")
print(abs(-273), pow(3, 4), int("101", 2))
Output
2h 34m 5s
273 81 5

Step-by-step explanation

  • divmod(9245, 60) returns (154, 5): 154 whole minutes and 5 seconds left over.
  • divmod(154, 60) returns (2, 34): 2 hours and 34 minutes.
  • Tuple unpacking stores both parts of each result in two names at once.
  • int("101", 2) reads the text as a binary number, which is 5.

Expected output

Running “Splitting seconds with divmod” prints exactly:

TextOutput
2h 34m 5s
273 81 5

Practical applications

  • E-commerce systems store money as integer paise to avoid rounding errors.
  • Cryptography uses Python’s huge integers for very large prime numbers.
  • Web developers use hexadecimal integers for colours such as 0xFF8800.

Best practices

  • Use underscores to make large numbers readable.
  • Store identifiers such as phone numbers as strings.
  • Use divmod() when you need both quotient and remainder.

Common mistakes

Leading zeros

Avoid

pin = 0475

Prefer

pin = "0475"

Leading zeros are not allowed in integers. Codes such as PINs and phone numbers are text, not numbers.

Expecting / to keep an int

Avoid

half = 10 / 2 # 5.0, a float

Prefer

half = 10 // 2 # 5, an int

/ always returns a float. Use // for whole-number division.

Key takeaways

  • int represents whole numbers exactly, with no size limit.
  • 0b, 0o and 0x write binary, octal and hex integers.
  • abs(), divmod() and pow() are common integer helpers.

Try it yourself

  1. 1
    Convert 100,000 seconds into days, hours, minutes and seconds with divmod().
  2. 2
    Print 255 in binary, octal and hexadecimal.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerWhat is printed?
Practise this topic