When you call range() with two arguments, the first argument is the starting number, and the second argument is the end (non-inclusive). So you're starting from len(list_of_numbers), which is 5, and you're ending at 6. So it just prints 5.

To get the results you want, the starting number should be 0, and the end should be len(list_of_numbers)+1. If you call it with one argument, that's the end, and 0 is the default start. So use

for i in range(len(list_of_numbers)+1):

or if you want to pass the start explicitly:

for i in range(0, len(list_of_numbers)+1):
Answer from Barmar on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_for_range.asp
Python Looping Through a Range
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3): ... Python For Loops Tutorial For Loop Through a String For Break For Continue ...
Discussions

language design - What is the reason python uses range in for loops? - Software Engineering Stack Exchange
Is there some philosophical reasoning behind why python uses this syntax: for x in range(1,11,3): instead of, for example, the BASIC syntax: for x = 1 to 10 step 3 If Python is supposed to be more More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
Is there a way to change the value of range in a for loop?
It's hard to give feedback if you don't tell us what you want to do. More on reddit.com
🌐 r/learnpython
26
10
June 20, 2023
Understanding Range() function in python. [Level: Absolute Beginner]
You can think of range as returning a sequence of numbers. Since we use the syntax for in : to loop or iterate over a sequence, naturally we can put a range as the sequence. Does that help? More on reddit.com
🌐 r/learnpython
23
4
August 6, 2024
In Python, how does a for loop with `range` work? - Stack Overflow
for number in range(1,101): print number Can someone please explain to me why the above code prints 1-100? I understand that the range function excludes the last number in the specified range, More on stackoverflow.com
🌐 stackoverflow.com
🌐
Snakify
snakify.org › for loop with range
For loop with range - Learn Python 3 - Snakify
To iterate over a decreasing sequence, we can use an extended form of range() with three arguments - range(start_value, end_value, step). When omitted, the step is implicitly equal to 1. However, can be any non-zero value.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-loop-through-a-range
Python - Loop Through a Range - GeeksforGeeks
July 23, 2025 - The most simple way to loop through a range in Python is by using the range() function in a for loop.
🌐
StrataScratch
stratascratch.com › blog › python-for-loop-range-function
How Does Python For Loop Range Function Work? - StrataScratch
November 5, 2025 - Here is how to implement a countdown with Python by using a for loop: import time print("Starting countdown...") for i in range(3, 0, -1): print(f"Please wait...
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3): ... Note: The else block will NOT be executed if the loop is stopped by a break ...
🌐
freeCodeCamp
freecodecamp.org › news › python-for-loop-for-i-in-range-example
Python For Loop - For i in Range Example
March 30, 2021 - If range() is called with only one argument, then Python assumes start = 0. The stop argument is the upper bound of the range. It is important to realize that this upper value is not included in the range.
Find elsewhere
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop - Python Wiki
For loop from 0 to 2, therefore running 3 times. for x in range(0, 3): print("We're on time %d" % (x))
Top answer
1 of 2
8

Note that range() is not actually part of the python language; it is a function. Having range be a function means you can plug any other function into a "for in" loop, including functions that don't increase monotonically, functions that lazy-execute, and functions that replace range() with something that better satisfies your personal sensibilities.

In other words, python's way affords you tremendous flexibility to do it however you want.

Your example is not exactly a fair one. It describes a scenario that would be very rare in practice. For a dice roll, it is simply

 for x in range(6)
2 of 2
6

There are multiple different ways to approach this answer (bold emphasis mine):

What is the reason python uses range in for loops?

This is not a for loop. It is a foreach loop. I.e. it is not a loop that loops over a pre-defined set of loop indices, it is an iterator that iterates over the elements of a collection.

In particular, in

for e in [2, 3, 5, 7, 11, 13, 17]:
    print(e)

The result will not be

0
1
2
3
4
5
6

but

2
3
5
7
11
13
17

What is the reason python uses range in for loops?

It doesn't. It uses an arbitrary expression. More precisely, an arbitrary expression that evaluates to an iterator or to something that can be implicitly converted to an iterator (such as an iterable):

class MyIterator:
    def __init__(self):
        self.counter = -1
        self.lost = [4, 8, 15, 16, 23, 42]

    def __next__(self):
        self.counter += 1

        if self.counter == 6:
            raise StopIteration

        return self.lost[self.counter]

class MyIterable:
    def __iter__(self):
        return MyIterator()

my_iterable = MyIterable()

for num in my_iterable:
    print(num)

# 4
# 8
# 15
# 16
# 23
# 42

Is there some philosophical reasoning behind why python uses this syntax

Yes: It is more general and thus makes the language simpler. The BASIC for loop can do one thing and one thing only: loop over a pre-defined set of loop indices. In fact, it is even more limited than that, because there are further restrictions on the loop indices: they need to be monotonically increasing or decreasing with a fixed step size.

If you want the indices to be non-monotonic, you need a new language construct. If you want the indices to have varying step sizes, you need a new language construct. If you want to iterate over the elements of a collection, you need a new language construct.

With Python's foreach loop, you can simply have a function that generates indices in whatever order you want, and loop over those. You can iterate over the elements of any arbitrary collection, and note that "collection" is interpreted very broadly.

Actually, you can iterate over the elements of any arbitrary iterator. An iterator can be something very general, and it doesn't even have to be finite, e.g. "all prime numbers".

As I have shown above, it is very easy to create your own custom iterators and iterables. It is in fact even more easy using generator functions:

def my_generator():
    yield 4
    yield 8
    yield 15
    yield 16
    yield 23
    yield 42

for num in my_generator():
    print(num)

# 4
# 8
# 15
# 16
# 23
# 42

And even more easy with generator expressions.

If Python is supposed to be more human readable than most languages, in this case particular case it seems to be worse than say Sinclair BASIC.

If you are looping over loop indices in Python (or any modern language, really), you are doing it wrong.

You should be using higher-level iterators instead, such as reduce (you may also know this one under the name fold or more general Catamorphism), accumulate (you may also know this one under the name scan or prefix-sum), cycle, chain, groupby, or product. Or, you should be using list / set / dictionary comprehensions, generator expressions, or algorithms and data structures supplied by the standard library or third-party libraries.

🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - In many languages, including C++, Java, and JavaScript, for loops are mainly based on indices. Python loops are different. In Python, a for loop is based on sequence elements instead of indices.
🌐
DataCamp
datacamp.com › tutorial › python-for-i-in-range
A Beginner's Guide to Python for Loops: Mastering for i in range | DataCamp
January 31, 2024 - ... Here, the item represents each element in the sequence, and the loop executes the code block for each element. The range() function in Python is pivotal for generating a sequence of numbers, offering enhanced control and flexibility in loops.
🌐
Learn Python
learnpython.org › en › Loops
Loops - Learn Python - Free Interactive Python Tutorial
For loops iterate over a given sequence. Here is an example: primes = [2, 3, 5, 7] for prime in primes: print(prime) For loops can iterate over a sequence of numbers using the "range" and "xrange" functions.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python for loop with range
A Basic Guide to Python for Loop with the range() Function
March 26, 2025 - Summary: in this tutorial, you’ll learn about the Python for loop and how to use it to execute a code block a fixed number of times. In programming, you often want to execute a block of code multiple times. To do so, you use a for loop. The following illustrates the syntax of a for loop: for index in range(n): statementCode language: Python (python)
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › range() in for loop in python
range() in for loop in Python - Spark By {Examples}
May 31, 2024 - How to use Python range() in for loop? The range() function is typically used with for loop to repeat a block of code for all values within a range. Let’s discuss different scenarios of using the range() function with for loop in Python.
🌐
PYnative
pynative.com › home › python › python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - The value of i is determined by the formula i = i + step. So it means range() produces numbers one by one as the loop moves to the next iteration. It saves lots of memory, which makes range() faster and more efficient.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-2-for-loop
5.2 For loop - Introduction to Python Programming | OpenStax
March 13, 2024 - In Python, a container can be a range of numbers, a string of characters, or a list of values. To access objects within a container, an iterative loop can be designed to retrieve objects one at a time. A for loop iterates over all elements in a container...
🌐
Python.org
discuss.python.org › ideas
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
August 10, 2022 - Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. There is an exception with this code: an infinite loop with a counter: i = 0 while True: ... if breaking_condition: break i += 1 Proposal: could we accept that range() without any parameter ...