liveBook · Manning

Range

As explained in Learn AI-Assisted Python Programming, Second Edition

The Python range function produces an integer sequence whose upper bound is exclusive.

Python’s range function produces integers from a starting value up to, but not including, an ending value. Thus, range(0, 9) produces 0 through 8; use range(0, 10) to include 9. section 1.1.1 section 5.1.1

Wrap range in list to see its values directly. In a loop, combine range with len to visit each valid index of a list or string. section 5.1.1 section 5.1.1

Loop through a list by index

lst = ['cat', 'dog', 'bird', 'fish'] for index in range(0, len(lst)): print('Got', lst[index]) print('Hello,', lst[index]) The indices are 0, 1, 2, and 3, matching the four valid positions in lst. The loop stops before len(lst), which is 4. section 5.1.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines the exclusive upper bound and demonstrates list conversion, len, indexing, and for-loop use.
Examples
Uses range(0, 9), list(range(3, 9)), and an indexed loop over cat, dog, bird, and fish.
What it assumes
Assumes basic Python loops, lists, strings, and indexing while explaining the exclusive endpoint from the beginning.
What you can do after
Apply it via Python to generate bounded integer sequences and iterate through collection indices.
How it connects
Connects range with for loops, lists, strings, indexing, and len.

Also explained in

Compare how 8 books explain range →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.