Because it's more common to call range(0, 10) which returns [0,1,2,3,4,5,6,7,8,9] which contains 10 elements which equals len(range(0, 10)). There's a tendency in programming to use 0-based indexing.

Also, consider the following common code snippet:

for i in range(len(li)):
    pass

Could you see that if range() went up to exactly len(li) that this would be problematic? The programmer would need to explicitly subtract 1. This also follows the common trend of programmers preferring for(int i = 0; i < 10; i++) over for(int i = 0; i <= 9; i++).

If you are calling range with a start of 1 frequently, you might want to define your own function:

>>> def range1(start, end):
...     return range(start, end+1)
...
>>> range1(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Answer from moinudin on Stack Overflow
Top answer
1 of 11
331

Because it's more common to call range(0, 10) which returns [0,1,2,3,4,5,6,7,8,9] which contains 10 elements which equals len(range(0, 10)). There's a tendency in programming to use 0-based indexing.

Also, consider the following common code snippet:

for i in range(len(li)):
    pass

Could you see that if range() went up to exactly len(li) that this would be problematic? The programmer would need to explicitly subtract 1. This also follows the common trend of programmers preferring for(int i = 0; i < 10; i++) over for(int i = 0; i <= 9; i++).

If you are calling range with a start of 1 frequently, you might want to define your own function:

>>> def range1(start, end):
...     return range(start, end+1)
...
>>> range1(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2 of 11
100

Although there are some useful algorithmic explanations here, I think it may help to add some simple 'real life' reasoning as to why it works this way, which I have found useful when introducing the subject to young newcomers:

With something like 'range(1,10)' confusion can arise from thinking that pair of parameters represents the "start and end".

It is actually start and "stop".

Now, if it were the "end" value then, yes, you might expect that number would be included as the final entry in the sequence. But it is not the "end".

Others mistakenly call that parameter "count" because if you only ever use 'range(n)' then it does, of course, iterate 'n' times. This logic breaks down when you add the start parameter.

So the key point is to remember its name: "stop". That means it is the point at which, when reached, iteration will stop immediately. Not after that point.

So, while "start" does indeed represent the first value to be included, on reaching the "stop" value it 'breaks' rather than continuing to process 'that one as well' before stopping.

One analogy that I have used in explaining this to kids is that, ironically, it is better behaved than kids! It doesn't stop after it supposed to - it stops immediately without finishing what it was doing. (They get this ;) )

Another analogy - when you drive a car you don't pass a stop/yield/'give way' sign and end up with it sitting somewhere next to, or behind, your car. Technically you still haven't reached it when you do stop. It is not included in the 'things you passed on your journey'.

I hope some of that helps in explaining to Pythonitos/Pythonitas!

๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - In Python, Using a for loop with range(), we can repeat an action a specific number of times. For example, letโ€™s see how to use the range() function of Python 3 to produce the first six numbers. ... Note: As you can see in the output, We got six integers starting from 0 to 5. If you notice, range() didnโ€™t include 6 in its result because it generates numbers up to the stop number but never includes the stop number in its result.
Discussions

Why Range() does not include last or end element ? Is it possible to include last element of range?
** Please check this question and give me an another way to this solve this problem. I had used the range in for loop but it could not able to include last element. So i had solved the problem but i want an another way to solve. Create a function named remove_middle which has three parameters ... More on discuss.codecademy.com
๐ŸŒ discuss.codecademy.com
0
0
June 15, 2020
Including the last value in a range loop Python 3 - Stack Overflow
I am having issues trying to get my code to print the last value of the range when I am running a loop. Here is the problem I was given with my code at the end: start_character = "A" More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - For loop range and interval, how to include last step - Stack Overflow
What is the best way to make a for loop such that for i in range(0,99,20) iterates over i = 0 20 40 60 80 99? rather than just i = 0 20 40 60 80? Right now I have an if statement to check if mo... More on stackoverflow.com
๐ŸŒ stackoverflow.com
list - How do I include the last element in a range in python? - Stack Overflow
I'd recommend you to not dereference the list in a range function in the first place, a better option would be the use of len(): ... That way you're making sure to loop through the whole list and reference every element. Note that this was more for explanatory reasons, the cleanest way is as ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Snakify
snakify.org โ€บ for loop with range
For loop with range - Learn Python 3 - Snakify
Pay attention that maximum value in range() is n + 1 to make i equal to n on the last step. 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. The loop always includes start_value and excludes end_value during iteration:
๐ŸŒ
Real Python
realpython.com โ€บ python-range
Python range(): Represent Numerical Ranges โ€“ Real Python
November 24, 2024 - In this range, you use the arguments to calculate number times each integer from one to ten. In particular, the last step argument makes sure that numbers in each row are correctly spaced out. To format ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ python โ€“ range() inclusive values
Python - range() Inclusive Values - Spark By {Examples ...
May 31, 2024 - # range() exclusively for i in range(20,11,-2): print(i,end=" ") print() # range() inclusively for i in range(20,11+1,-2): print(i,end=" ") # Output: # 20 18 16 14 12 # 20 18 16 14 ยท We are returning a range of values exclusively and inclusively with a decrement of 2. We have seen the exact difference between the inclusive and exclusive range of values returned by the range() function in python.
๐ŸŒ
Codecademy Forums
discuss.codecademy.com โ€บ get help โ€บ python
Why Range() does not include last or end element ? Is it possible to include last element of range? - Python - Codecademy Forums
June 15, 2020 - I had used the range in for loop but it could not able to include last element. So i had solved the problem but i want an another way to solve. Create a function named remove_middle which has three parameters named lst , start , and end .
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-use-range-with-inclusive-end-466082
Python - How to use range with inclusive end
At LabEx, we recommend mastering range() as a fundamental skill for Python programming, especially when dealing with loops and sequence generation. Python's standard range() function does not natively support inclusive end values. Developers ...
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-range
How to Use Range in Python | Coursera
February 24, 2023 - The range function is not inclusive in Python. By default, range does not include the last value. ... Python range does not support the float type, it only works with integers. Since two of the three parameters for Python range() are optional, ...
Find elsewhere
๐ŸŒ
Flexiple
flexiple.com โ€บ python โ€บ inclusive-range-python
Inclusive range Python - What does this signify? | Flexiple - Flexiple
March 14, 2022 - This makes the Python range function inclusive. The code is as follows: def range_inclusive(start, end): return range(start, end+1) range_inclusive(1, 10) When the function is called, it adds 1 to the stop parameter. The output is as follows.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ understanding the built-in python range() function
Understanding the built-in Python range() function - AskPython
January 25, 2026 - If you need to skip iterations or jump around, use a while loop instead. Another common error is forgetting that the stop value is exclusive, leading to off-by-one bugs. # Wrong: misses the last item items = [10, 20, 30, 40, 50] for i in range(len(items) - 1): print(items[i]) # Outputs: 10, 20, 30, 40 (missing 50) # Right: includes all items for i in range(len(items)): print(items[i]) You can use the in operator to check if a number exists in a range object. Python calculates this mathematically instead of iterating through every value, making it extremely fast.
Top answer
1 of 3
4

well, from the help:

>>> help(range)
range(...)
    range([start,] stop[, step]) -> list of integers

    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.

so the last increment is not stop, but the last step before stop.

  • in countMe shouldn't the code go up till 18 ;
  • why is the last number printed in countMe 15, and not 18 ;
  • why is that in the second function oddsOut the function only founts till 7 for j and not 8 even though j is 8 ;
  • why is the last number printed in oddsOut 14.

more generally speaking the answer to those questions is that in most of the languages, a range is defined as [start:stop[, i.e. the last value of the range is never included, and the indexes start always at 0. The mess being that in a few languages and when working on algorithmics, ranges start at 1 and are inclusive with the last value.

In the end, if you want to include the last value you can do:

def closed_range(start, stop, step=1):
    return range(start, stop+1, step)

or in your example:

>>> def countMe(num):
>>>     for i in range(0, num+1, 3):
>>>         print (i)
>>> 
>>> countMe(18)
0
3
6
9
12
15
18
>>> 
2 of 3
2

The stop parameter in a range does not include that number for example

for i in range(0,5):
    print i

would print 0-4 but not 5.

๐ŸŒ
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. By default, the range() function generates numbers starting from 0 up to, but not including, the specified endpoint.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_for_range.asp
Python Looping Through a Range
The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6): ... 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 For Else Nested Loops For pass
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-for-loop-for-i-in-range-example
Python For Loop - For i in Range Example
March 30, 2021 - As discussed in Python's documentation, for loops work slightly differently than they do in languages such as JavaScript or C. A for loop sets the iterator variable to each value in a provided list, array, or string and repeats the code in the body of the for loop for each value of the iterator variable. In the example below, we use a for loop to print every number in our array. # Example for loop for i in [1, 2, 3, 4]: print(i, end=", ") # prints: 1, 2, 3, 4,
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ looping until the second to last element in a list, what's best practice?
r/learnpython on Reddit: Looping until the second to last element in a list, what's best practice?
August 27, 2023 -

edit: forgot to mention I want the indeces not just the items

the methods I can think of are:

for i in range(len(arr) - 1)

for i, e in enumerate(arr[:len(arr) - 1])

I know range(len(arr)) is frowned upon, but I don't see how it's worse than enumerate in this case. In fact using enumerate on a shortened list and calling len to find the second to last element of that list seems extremely clunky and far less readable.

What's the best practice for doing this?