Several things to note:

  1. Python is zero-indexed
    This explains the IndexError when executing myList[6] because the last element is #5 not #6.
  2. range(start, stop) is "front-inclusive" and "end-exclusive"
    So while iterating, you print myList[0] but not myList[6], which is why you did not get an IndexError here.
  3. range(n)
    n represents the number of times to execute the for loop, so because you would like to print 6 elements, you should do range(len(myList)) without -1.
  4. range(start, stop, step)
    Again, Python's for loops are "front-inclusive" and "end-exclusive" so you should start with the index of the last element (len(myList)-1) and end with the index of the first element minus 1 (-1). Step size can be thought of as the "displacement" while iterating, so with a step size of -1, you decrease the iterator i by one at each epoch.

Hope this helped!

Answer from ccl on Stack Overflow
🌐
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. In the example below, we have a range starting at the default value of 0 and including integers less than 5. # Example with one argument for i in range(5): print(i, end=", ") # prints: 0, 1, 2, 3, 4,
Discussions

What's the difference between for i in list: vs for i in range(len(list)):
>>> l = [ 'a', 'b', 'c', 'X', 'Y', 'Z' ] >>> for i in l: ... print(i) ... a b c X Y Z >>> for i in range(len(l)): ... print(i) ... 0 1 2 3 4 5 range(x) just gives you an object that yields the integers from 0 to x - 1 when iterated. More on reddit.com
🌐 r/AskProgramming
11
0
July 3, 2021
What does "for i in range" mean in Python?
Use a 'code block' in the 'new' reddit to paste code so we can see indents which are vital to python! for i in range(n + 1): sum = sum + i * i * i when you say 'for i in range(n + 1)' you are creating a variable called 'i' and setting it equal to the first value in the second part of the line called range(). every time the loop loops, i becomes the next value in the second part of the line (in this case range()) Check this code out to understand it: my_list = ['potato', 'pineapple', 'strawberry', 'banana', 'orange'] for var in my_list: #instead of 'i' i used 'var' you can use any name you want, since you are creating the variable. var is = to a value in my_list, and will go to the next value every time the loop loops. This will run a total of 5 times, because there are 5 items in the list we are looping through (my_list) print(var) now put that in your terminal/whatever and see the output, itll look like this: potato pineapple strawberry banana orange its the same with range() -- example: range(5) just means every number between 0 and 5, including 0 but not 5. so range(5) has 5 items, 0, 1, 2, 3 and 4. our loop should run 5 times: for i in range(5): print(i) you should see the output: 0 1 2 3 4 if you ever want to know more about certain parts of python, google it, for example: 'python range()' will give you tons of results that are helpful. More on reddit.com
🌐 r/learnpython
15
18
September 9, 2019
Is range(len(list)) ever Pythonic?
On a related note, you should never name your variable list. More on reddit.com
🌐 r/learnpython
79
83
September 9, 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
🌐
Python Guides
pythonguides.com › for-i-in-range-python
For i in range() in Python: What It Actually Means
September 29, 2026 - A comprehension builds a list in one expression instead of appending in a loop. The i for i in range(5) part on its own produces the numbers unchanged, so list(range(5)) does the same job more plainly.
🌐
W3Schools
w3schools.com › python › python_lists_loop.asp
Python - Loop Lists
Learn more about for loops in our Python For Loops Chapter. You can also loop through the list items by referring to their index number. Use the range() and len() functions to create a suitable iterable.
🌐
GeeksforGeeks
geeksforgeeks.org › python › range-to-a-list-in-python
range() to a list in Python - GeeksforGeeks
September 22, 2026 - The * operator unpacks the values from the range object. The unpacked values are placed inside a new list. A for loop can manually add each value from the range to an empty list.
🌐
Fonzi AI
fonzi.ai › blog › python-for-i-in-range
How to Use "for i in range()" in Python (With Clear Examples)
August 14, 2025 - This method enhances readability ... position. Using Python’s zip() function with for i in range() allows you to iterate over multiple sequences in parallel....
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › what does "for i in range" mean in python?
r/learnpython on Reddit: What does "for i in range" mean in Python?
September 9, 2019 -

So, I had a question where I was supposed to find the sum of the first natural numbers using Python.

Here's the problem:

Write a program to find the sum of the cubes of the first n natural numbers, where the value of n is provided by the user.

And this is the code that my professor used to allow the interpreter to produce the result:

= int(input("Enter a number:"))

sum = 0

for i in range (n+1): sum = sum + i*i*i

# for i in range starts as a loop, and then tries to get to (n + 1), whatever that may be

print("the sum of the first", n, "integers is", sum)

However, I can't seem to understand what "for i in range (n + 1): sum = sum + i * i * i" means. In other words, I don't understand what role this part of the code is doing to produce the result. Especially, I don't understand what the role of "for i in range (n + 1)" is doing. Does anyone mind explaining this to me?

Top answer
1 of 6
23
Use a 'code block' in the 'new' reddit to paste code so we can see indents which are vital to python! for i in range(n + 1): sum = sum + i * i * i when you say 'for i in range(n + 1)' you are creating a variable called 'i' and setting it equal to the first value in the second part of the line called range(). every time the loop loops, i becomes the next value in the second part of the line (in this case range()) Check this code out to understand it: my_list = ['potato', 'pineapple', 'strawberry', 'banana', 'orange'] for var in my_list: #instead of 'i' i used 'var' you can use any name you want, since you are creating the variable. var is = to a value in my_list, and will go to the next value every time the loop loops. This will run a total of 5 times, because there are 5 items in the list we are looping through (my_list) print(var) now put that in your terminal/whatever and see the output, itll look like this: potato pineapple strawberry banana orange its the same with range() -- example: range(5) just means every number between 0 and 5, including 0 but not 5. so range(5) has 5 items, 0, 1, 2, 3 and 4. our loop should run 5 times: for i in range(5): print(i) you should see the output: 0 1 2 3 4 if you ever want to know more about certain parts of python, google it, for example: 'python range()' will give you tons of results that are helpful.
2 of 6
3
range is a function that returns a list of numbers (in the range passed as argument) So when you write "for i in range(x)" you mean: "for each ITEM in LIST: do something" For each item in the list passed after the IN operator, code will run the next codes you wrote. And inside the loop, the current item is identifyed as "I" Like: my_list = ["Nice","Code","Dude","!"] for item in my_list: print(item) In other words: for in : actions for each item
🌐
W3Schools
w3schools.com › python › ref_func_range.asp
Python range() Function
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training · ❮ Built-in Functions · Create a sequence of numbers from 0 to 5, and print each item in the sequence: x = range(6) for n in x: print(n) Try it Yourself » ·
🌐
Codecademy
codecademy.com › forum_questions › 558ae27493767630000004f5
"for x in list" verses "for x in range(len(list)) | Codecademy
for i in range() iterates through numbers. for element in my_list iterates through whatever is in the list (they could be numbers, too) for i in range(len(a_list)) iterates through numbers (which can be used for index access of a_list).
🌐
Medium
medium.com › @tamarainwang › when-to-use-for-i-in-range-vs-a68a6f32da9f
When to Use for i in range () for loop vs. for item in iterable () for loop in Python: A Guide with Creative Employee Benefits | by Tamara Inwang | Medium
July 27, 2024 - In this instance, len(perks) returns the length of the list, perks which is 5. range () is also a function. It generates a sequence of numbers which you can loop over. Below is its syntax: ... The start argument is the first value in the range. If you call range() with only one argument, Python assumes start is 0.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() - Stanford Computer Science
The 3 parameter form begins with start number, up to but no including the stop number as usual. The difference is the "step" amount between numbers is now custom. Once the number is equal or goes beyond the stop, the range ends. As before, the stop number itself is always omitted. >>> list(range(0, 10, 2)) [0, 2, 4, 6, 8] >>> list(range(0, 10, 6)) [0, 6] >>> list(range(200, 800, 100)) [200, 300, 400, 500, 600, 700]
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
1 week ago - You can create ranges by calling range() with one, two, or three arguments, as the following examples show: ... >>> list(range(5)) [0, 1, 2, 3, 4] >>> list(range(1, 7)) [1, 2, 3, 4, 5, 6] >>> list(range(1, 20, 2)) [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
🌐
Mimo
mimo.org › glossary › python › range-function
Python range(): start, stop and step in Loops
Use Python range() in for loops: control sequences with start, stop, and step, count down with a negative step, and convert a range into a list.
🌐
DataCamp
datacamp.com › home › tutorials › python › a beginner's guide to python for loops: mastering for i in range
A Beginner's Guide to Python for Loops: Mastering for i in range | DataCamp
January 31, 2024 - This tutorial sheds light on the Python for i in range loop, a fundamental construct in Python that simplifies repetitive tasks. We'll embark on a journey to understand its syntax, versatility, and practical applications. A for loop in Python is a control structure used to iterate over a sequence (like a list, tuple, dictionary, or string).
🌐
University at Buffalo
math.buffalo.edu › ~badzioch › MTH337 › PT › PT-lists.html
Lists, ranges and “for” loops — MTH 337
It would be inconvenient having to manually type a list of integers to code such iteration. Instead we can use the range function that generates integers in a given range. range(n) generates consecutive integers, starting at 0 and ending at n-1: ... Note. In Python 3 the range function does not produce a list of integers, but instead generates integers one by one as needed.
🌐
PYnative
pynative.com › home › python › python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - Next, In every subsequent iteration of for loop, the value of i is incremented by the step value. 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.
🌐
Python Software Foundation
wiki.python.org › python › ForLoop.html
For Loop - Python Wiki
When you have a block of code you ... as a "nested loop". In Python, these are heavily used whenever someone has a list of lists - an iterable object within an iterable object. for x in range(1, 11): for y in range(1, 11): print('%d * %d = %d' % (x, y, x*y))...
🌐
AskPython
askpython.com › python › built-in-methods › python-iterate-range-starting-at-1
Pythonic Way to Iterate Through a Range Starting at 1 - AskPython
June 30, 2023 - Let’s again try to understand it with the code itself. Related: Learn list comprehensions in depth. ... To create a list through list comprehension we’ll simply input the starting value argument in the range function like before. ... Now that we’ve learned the enumerate function and the list comprehension let’s combine both tools and create a list of tuples with index-value pairs of all the numbers from 1-9 with index 1-9. indexed_list = [(index, value) for index, value in enumerate(range(1,10), start=1)] print(indexed_list)
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-range.html
Python range() Function
>>> s = 'Python' >>> len(s) 6 >>> for i in range(len(s)): ... print(s[i]) ... P y t h o n · Below are some more examples calling range(). For cosmetic reasons in the examples below, the call of the range() function is inside a list() so the numbers will print out.