for i in range(15):
    print i #will print out 0..14

for i in range(1, 15):
    print i # will print out 1..14


for i in range (a, b, s):
    print i # will print a..b-1 counting by s. interestingly if while counting by the step 's' you exceed b, it will stop at the last 'reachable' number, example

for i in range(1, 10, 3):
    print i

> 1
> 4
> 7

List Splicing:

a = "hello" # there are 5 characters, so the characters are accessible on indexes 0..4

a[1] = 'e'
a[1:2] = 'e' # because the number after the colon is not reached.

a[x:y] = all characters starting from the character AT index 'x' and ending at the character which is before 'y'

a[x:] = all characters starting from x and to the end of the string

In the future, if you ever wonder what the behavior of python is like, you can try it out in the python shell. just type python in the terminal and you can enter any lines you want (though this is mostly convenient for one-liners rather than scripts).

Answer from Jeremy Fisher on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_lists_access.asp
Python - Access List Items
When specifying a range, the return value will be a new list with the specified items. ... thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5]) Try it Yourself » · Note: The search will start at index 2 (included) and end at index 5 (not included).
Discussions

Is range(len(list)) ever Pythonic?
I will do it when (1) I need multiple indexes for each iteration (2) The iterations should skip some elements i.e. first and last (3) I don't actually need the value, just the index e.g. something like this for i in range(1, len(array) - 1): array[i] = (array[i-1] + array[i+1]) / 2 More on reddit.com
🌐 r/learnpython
79
83
September 9, 2022
'List index out of range' when it is not.
learn how to use a debugger and see what that list looks like at that particular line of code More on reddit.com
🌐 r/learnpython
22
1
March 27, 2021
Irksome list slicing syntax - Why does python interpret indices differently in 'direct' indexing vs. 'range' indexing?
Because it provides some really nice invariants. For example, list[m:n] + list[n:k] == list[m:k] and len(list[m:n]) == n - m. More on reddit.com
🌐 r/learnpython
11
3
October 23, 2018
Embarrassingly, i don't understand how list indexing works
Let us step back a little. Before understanding your example, try to grok the example below for i in range(0, 3): print(i) which results in: 0 1 2 And the example below my_list = [42, 6, 2024] for i in range(0, 3): print(i, my_list[i]) Which results in: 0 42 1 6 2 2024 Are those clear to ya? Thanks! More on reddit.com
🌐 r/learnpython
25
14
August 31, 2024
🌐
Railsware
railsware.com › home › engineering › indexing and slicing for lists, tuples, strings, other sequential types in python
Python Indexing and Slicing for Lists, Tuples, Strings, other Sequential Types | Railsware Blog
January 22, 2025 - Python supports slice notation for any sequential data type like lists, strings, tuples, bytes, bytearrays, and ranges. Also, any new data structure can add its support as well. This is greatly used (and abused) in NumPy and Pandas libraries, which are so popular in Machine Learning and Data Science. It’s a good example of “learn once, use everywhere”. In this article, we will focus on indexing ...
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() Function
The most common form is range(n), given integer n returns a numeric series starting with 0 and extending up to but not including n, e.g. range(6) returns 0, 1, 2, 3, 4, 5. With Python's zero-based indexing, the contents of a string length 6, are at index numbers 0..5, so range(6) will produce ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-index
Python List index() - Find Index of Item - GeeksforGeeks
It works by searching through the list from the beginning and returning the index (position) of the first occurrence of the element you're looking for. Example: ... Explanation: index("dog") method finds the first occurrence of "dog" in the ...
Published   April 27, 2025
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. It raises an IndexError if the list is empty or the index is outside the list range.
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - So the syntax is: list_name.index(element, start, stop). Here the start and stop values are optional. In fact, only use it when entirely sure about the range; otherwise, you will get a ValueError, as shown below.
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › range-function
Python range() Function [Python Tutorial]
Using range() to iterate over indices rather than elements avoids creating additional lists and optimizes memory. Particularly in large lists of large objects, accessing items by index can be much more efficient with range(). ... Python 2 has a built-in function called xrange() to create ranges.
🌐
freeCodeCamp
freecodecamp.org › news › python-range-function-explained-with-code-examples
Python range() Function – Explained with Code Examples
October 6, 2021 - This is why it's convenient to use range() to loop through iterables. An iterable of length len has 0, 1, 2, ..., len-1 as the valid indices. So to traverse any iterable, all you need to do is to set the stop value to be equal to len.
🌐
HackerEarth
hackerearth.com › practice › notes › samarthbhargav › a-quick-intro-to-indexing-in-python
A Quick intro to Indexing in Python - Samarth Bhargav
In this section, I'm gonna list a few list indexing idioms that I've found useful. my_list = range(10) new_list = my_list[:] # Create a duplicate list reverse_list = my_list[::-1] # Reverse
🌐
CodeWithHarry
codewithharry.com › tutorial › python-list-indexes
List Indexes | Python Tutorial | CodeWithHarry
The first item has index [0], the second item has index [1], the third item has index [2], and so on. ... As we have seen that list items have an index, we can access items using these indexes.
🌐
LabEx
labex.io › tutorials › python-how-to-use-index-range-in-python-lists-435401
How to use index range in Python lists | LabEx
In Python, lists are ordered collections of elements that can be accessed using index positions. Each element in a list has a unique index, starting from 0 for the first element.
🌐
Reddit
reddit.com › r/learnpython › 'list index out of range' when it is not.
r/learnpython on Reddit: 'List index out of range' when it is not.
March 27, 2021 -

I'm new to python so please be gentle.

I have a list of 9 float numbers called scorelist. All of these numbers are separate.

[23.0, 39.0, 47.0, 51.5, 53.0, 58.5, 78.0, 80.5, 9.0]    

Part of the program needs to determine the median number.

I wrote

mid = scorelist[4] 

because that's the middle number and is therefore the median. However, despite this not being out of range, I get the 'list index out of range' error for that line.

I literally am not doing anything to change or alter the list in the entire code. The only thing I ever do to change the list is when I assign a slice of it to another list, but this doesn't actually change the original list, it just copies the numbers (printing before and after gives the original list).

Printing the list before and after declaring the list is fine. Printing 'mid' also works and gives the right number. It even appears to correctly use itself in an elif block.

But because all of this is inside of a for loop, my compiler crashes as soon as the loop ends and doesn't continue to the code that's outside the loop.

Why is this happening? How can I fix it?

Google is extremely unhelpful and just says "the number must be out of range" but it's literally not. It's working like it should be, but I still get the error for that line and the code stops executing.

I'll post the full code if it's needed. I don't know what would be wrong.

🌐
Quora
quora.com › How-do-I-select-a-range-of-numbers-in-an-array-in-Python
How to select a range of numbers in an array in Python - Quora
a[-5 : -1] # will return array [f, g, h, i]. Remember the ending_index (-1th index) element is excluded which is ‘j’ ... This extracting specific range values from an array is called ‘slicing’. This works same for an array(1-d), list or series in python.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-slicing
Python List Slicing - GeeksforGeeks
a = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Get elements starting from index -2 # to end of list b = a[-2:] print(b) # Get elements starting from index 0 # to index -3 (excluding 3th last index) c = a[:-3] print(c) # Get elements from index -4 # to -1 ...
Published   July 23, 2025
🌐
Medium
medium.com › @tuenguyends › pythons-list-type-part-1-indexing-and-slicing-23ad68ca4c66
Python’s list type (part 1) — Indexing and slicing | by Tue Nguyen | Medium
April 15, 2022 - We can index a list by counting from left to right (using positive index). ... If we try to access outside the range of a list, we will get a list index out of range error.
🌐
Reddit
reddit.com › r/learnpython › irksome list slicing syntax - why does python interpret indices differently in 'direct' indexing vs. 'range' indexing?
r/learnpython on Reddit: Irksome list slicing syntax - Why does python interpret indices differently in 'direct' indexing vs. 'range' indexing?
October 23, 2018 -

Here is what I don't quite get:
Given a list

list = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

We access the mth or nth element thusly:

m = 1
n = 5
print(list[m] + ', ' + list[n])
> B, F

Everything makes sense so far.

Now I want to access a subset of the elements of 'list' that includes all elements from the mth to the nth element (inclusively).

What I expect the notation to look like

slice = list[m:n]

does not produce the result

print(slice)
> ['B' 'C' 'D' 'E' 'F']

but rather, produces

print(slice)
> ['B' 'C' 'D' 'E']

Why does n suddenly become exclusive when written as a 'range'? Why does it make sense to write

slice = list[m:(n+1)]

to produce a subset of a list that spans the mth to the nth element?

As a bit of background, I have past experience in VHDL/Verilog, where a range written as m:n inclusively specifies bits m to n of a signal or register. The 'python way' of making the n exclusive baffles me. I'd like to understand why python syntax works this way.

🌐
Scaler
scaler.com › home › topics › how to fix indexerror - list index out of range in python
How to Fix IndexError - list index out of range in Python | Scaler Topics
February 8, 2024 - Python lists are zero-indexed, so a list with n elements has indexes from 0 to n-1. Negative indexes can be used to access elements from the end of the list, with -1 being the last element.
🌐
freeCodeCamp
freecodecamp.org › news › slicing-and-indexing-in-python
Slicing and Indexing in Python – Explained with Examples
December 11, 2025 - In Python, indexing starts from 0, which means the first element in a sequence is at position 0, the second element is at position 1, and so on. To access an element in a sequence, you can use square brackets [] with the index of the element ...