Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python range() and enumerate()

Control flow · Tutorial 12

Python range() and enumerate()

Generate number sequences with range and track positions with enumerate while iterating.

Open the compilerPractice exercises
beginner7 min readUpdated November 19, 20263 learning goals
On this page
  1. The three forms
  2. Counting from one
  3. Summing with a step
  4. enumerate()
  5. zip()
  6. range is not a list
  7. Practice

What you will learn

  • Use range(start, stop, step)
  • Convert a range to a list for inspection
  • Get index and value together with enumerate
Before this lesson: Python for Loops

range() creates a sequence of integers without storing them all in memory. It is commonly used with for.

The three forms#

Python
print(list(range(5)))        # [0, 1, 2, 3, 4]
print(list(range(2, 6)))     # [2, 3, 4, 5]
print(list(range(1, 10, 2))) # [1, 3, 5, 7, 9]
print(list(range(5, 0, -1))) # [5, 4, 3, 2, 1]
  • The stop value is excluded.
  • The default start is 0.
  • The default step is 1.
  • A negative step counts down.

range() is lazy. list() materialises the values, which is useful for inspection but unnecessary in a loop.

Counting from one#

Python
for position in range(1, 4):
    print(f"Item {position}")
Expected output

Item 1 Item 2 Item 3

Do not add 1 inside the loop to make the output one-based if the loop variable only controls display. Start the range at 1 instead.

Summing with a step#

Python
total = 0

for number in range(3, 31, 3):
    total += number

print(total)  # 165

This adds 3, 6, 9, ..., 30.

enumerate()#

When you need both the index and the value, use enumerate() instead of manually maintaining a counter:

Python
tasks = ["write", "test", "deploy"]

for index, task in enumerate(tasks):
    print(index, task)
Expected output

0 write 1 test 2 deploy

Start numbering at 1 when presenting positions to people:

Python
for position, task in enumerate(tasks, start=1):
    print(f"{position}. {task}")

Avoid index loops when you can iterate directly

Prefer for item in items: over for i in range(len(items)): print(items[i]). Use enumerate() only when the index is genuinely needed.

zip()#

zip() combines parallel iterables:

Python
names = ["Ada", "Grace", "Linus"]
scores = [90, 95, 88]

for name, score in zip(names, scores):
    print(f"{name}: {score}")

It stops at the shortest input. If you need to detect mismatched lengths, check them first.

range is not a list#

Python
numbers = range(5)
print(numbers)          # range(0, 5)
print(list(numbers))    # [0, 1, 2, 3, 4]
numbers[0] = 99         # TypeError

A range supports indexing and length but is immutable.

Practice#

Print a 5-row number triangle using range(). Then print a numbered list of five fruits with enumerate(..., start=1). Finally, use zip() to pair each fruit with a price and print a simple receipt.

Previous tutorialPython for LoopsNext tutorialPython break, continue, and Loop else

Related tutorials

beginner

Python break, continue, and Loop else

Control loop execution with break and continue, and learn how the rarely used loop else clause works.

6 min read
beginner

Python Lists: Creation, Indexing, and Iteration

Store ordered collections in lists, access items by position, iterate over them, and use slicing and unpacking.

9 min read

On this page

  1. The three forms
  2. Counting from one
  3. Summing with a step
  4. enumerate()
  5. zip()
  6. range is not a list
  7. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.