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

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
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
Hi, Usually in Python we can avoid ... 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 ... More on discuss.python.org
🌐 discuss.python.org
4
August 10, 2022
Loops - for i and for j in range(n) explained
It may seem special, but its just a normal for loop. You can call the range() function separately from these loops to generate a range object. These are iterables, and can be converted into lists, tuples, etc. Try doing something like print(list(range(5))), and it will show [0, 1, 2, 3, 4]. You can think of it just being a normal list, like this: for i in range(5): print(i) for i in [0, 1, 2, 3, 4]: print(i) So to answer your other question about for j in range(i), think about it this way. for i in range(3): for j in range(i): print(j) The first time this runs, i will be 0 and it will increase by 1 every time it the loop runs, so it will be running for i in range(0) the first time, for i in range(1) the second time, and so on. range() accepts 3 arguments, start, stop, and step. Lets do some examples list(range(10)) # stop at 10 # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list(range(5, 10)) # start at 5, end at 10 # [5, 6, 7, 8, 9] list(range(0, 10, 2)) # start at 0, end at 10, increment by 2 each time # [0, 2, 4, 6, 8] Hopefully this explains it a bit more! More on reddit.com
🌐 r/learnpython
6
8
July 3, 2021
Can someone explain how range() works?
It used to work that way, in Python 2. But it was pretty inefficient; if you did range(1000000) it would have to create a list with a million numbers. So in Python 3 it was changed to be lazy. It's now similar to a generator, in that you can create an iterator from it which will give the next number each time you call it: x = range(10) it = iter(x) next(it) # 0 next(it) # 1 etc. Note that this is exactly what happens behind the scenes when you put anything into a for loop. It's a bit cleverer than a standard generator though as it also has comparison operators so you can do things like if 3 in range(1, 10). More on reddit.com
🌐 r/learnpython
30
40
March 9, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-loop-through-a-range
Python - Loop Through a Range - GeeksforGeeks
March 27, 2026 - It works seamlessly with ranges of any size. Since the range() function only supports integers, creating a range of floating-point numbers requires external libraries or custom solutions. Here we will use numpy.arrange(). ... import numpy as np # Loop through floating-point numbers for i in np.arange(0.5, 2.5, 0.5): print(i, end=" ")
🌐
Mimo
mimo.org › glossary › python › for-loop
Python For Loop: Syntax and Examples [Python Tutorial]
The function call in the code block prints each number in the list. range() is a built-in function that creates a list of numbers starting at 0 and stopping before a specified integer. This makes the range function ideal for creating Python for loops with index variables.
🌐
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.
Find elsewhere
🌐
StrataScratch
stratascratch.com › blog › python-for-loop-range-function
How Does Python For Loop Range Function Work? - StrataScratch
November 5, 2025 - Loop starts: Python asks range(1, 6) for the first number → gets 1 ... We will solve three different challenges by using for loops and range together. Let’s start with the mathematical challenge.
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - Since a range is a sequence, you typically use for loops to iterate over ranges. In many languages, including C++, Java, and JavaScript, for loops are mainly based on indices. Python loops are different.
🌐
YouTube
youtube.com › watch
for loops with range() | Intro to CS - Python | Khan Academy - YouTube
Learn how to use for loops with range() to repeat a block of code a fixed number times. Trace how the computer updates the loop variable during execution.Vie...
Published   July 9, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-for-loops
Python For Loops - GeeksforGeeks
... Here, for loop is used to iterate through each character of the string and prints them one by one. ... range() function is used with for loops to generate a sequence of numbers.
Published   May 8, 2026
🌐
Quora
quora.com › Can-you-explain-the-difference-between-for-and-range-loops-in-Python-When-is-it-appropriate-to-use-each-one
Can you explain the difference between 'for' and 'range' loops in Python? When is it appropriate to use each one? - Quora
Answer (1 of 2): Range is not a loop. It is an iterator, which is a thing that returns one number each time it's called. It's most often used to control a for loop. Example: range(5) gives out five integers, from 0 to 4, one at a time. [code] x = [0,1,2,3,4] for n in x: print(n) for n in ran...
🌐
Geek University
geek-university.com › home › python › use for loop with the range() function
Use for loop with the range() function | Python#
February 1, 2022 - for i in range(1, 11): print('This is the', i, 'iteration.') When run, the code above gives the following output: >>> This is the 1 iteration. This is the 2 iteration. This is the 3 iteration.
🌐
Coursera
coursera.org › tutorials › python-range
How to Use Range in Python | Coursera
Use start, stop, and step arguments with range() to write a program that returns 100, 75, 50, 25. ... To get the required output, you'd need to set your start value to 100, your stop value to 0, and your step argument to -25.
🌐
Tutorial Gateway
tutorialgateway.org › python-for-loop
Python For Loop range
March 25, 2025 - Python For Loop executes a block of statements n number of times until a condition is false and the For loop range is mostly used in realtime
🌐
Codecademy
codecademy.com › forum_questions › 522215b7f10c60e27d0017de
Why should we use range in for loops? | Codecademy
n = [3, 5, 7] def total(List): total_sum = 0 for item in range(len(List)): total_sum += List[item] return total_sum print total(n)
🌐
Better Programming
betterprogramming.pub › stop-using-range-in-your-python-for-loops-53c04593f936
Stop Using range() in Your Python for Loops | by Jonathan Hsu | Better Programming
January 20, 2020 - Stop Using range() in Your Python for Loops How to access the current index using the enumerate() function The for loop. It is a cornerstone of programming — a technique you learn as a novice and …
🌐
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” ...
🌐
Profound Academy
profound.academy › python-introduction › for-loop-with-range-7UAYz2A3hQNuezXc5eAc
for loop with range() - Introduction to Python
November 2, 2024 - Introduction to Python · Description · We can use for loops with any iterable like range(): for index in range(5): print(f'Input the next number {index}:') n = int(input()) print(f'Great!
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node58.html
for loops and the range function
If you're familiar with other ... like this are solved by iterating over a sequence of integers created by the range function, and then refering to the individual elements of the sequence inside the body of the for loop to perform the desired tasks....