From the documentation:

range([start], stop[, step])

The start defaults to 0, the step can be whatever you want, except 0 and stop is your upper bound, it is not the number of iterations. So declare n to be whatever your upper bound is correctly and you will not have to add 1 to it.

e.g.

>>> for i in range(1, 7, 1): print(i)
... 
1
2
3
4
5
6
>>> for i in range(1, 7, 2): print(i)
... 
1
3
5

A nice feature, is that it works in reverse as well.

>>> for i in range(7, 0, -1): print(i)
... 
7
6
5
4
3
2
1

If you aren't using it as an index but for something that can have positive or negative values, it still comes in handy:

>>> for i in range(2, -3, -1): print(i)
... 
2
1
0
-1
-2
>>> for i in range(-2, 3, 1): print(i)
... 
-2
-1
0
1
2
Answer from Rolf of Saxony on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_range.asp
Python range() Function
Python Examples Python Compiler ... Training ... The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-for-loop-for-i-in-range-example
Python For Loop - For i in Range Example
March 30, 2021 - # More complex example for i in [1, 3, 5, 7, 9]: x = i**2 - (i-1)*(i+1) print(x, end=", ") # prints 1, 1, 1, 1, 1, When the values in the array for our for loop are sequential, we can use Python's range() function instead of writing out the contents of our array.
Discussions

python - Pythonic way to iterate through a range starting at 1 - Stack Overflow
Sign up to request clarification or add additional context in comments. ... range(1, n+1) is not considered duplication, but I can see that this might become a hassle if you were going to change 1 to another number. ... Nice answer to the question but have we really moved forward from range(1, n+1) 2015-10-22T15:44:35.227Z+00:00 ... I wouldn't say so. I just had this cool generator idea which seemed really pythonic ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Interpret "for i in n" as "for i in range(n)" if n is an int - Ideas - Discussions on Python.org
I expect that this idea will be shot down ๐Ÿ™‚ but Iโ€™m curious to hear why. As a pure convenience / syntactic sugar thing, Iโ€™d love not having to write โ€œfor i in range(n)โ€ ever again and instead be able to say โ€œfor i in nโ€ with no ambiguity (at least when n is an integer at runtime). More on discuss.python.org
๐ŸŒ discuss.python.org
1
January 17, 2024
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
13
17
September 9, 2019
python - Understanding range() in a for loop - Stack Overflow
Looking at your code, it's a simple mistake - you missed a closing bracket. Use this: ... You need to close the brackets around range() i.e. 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.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-range.html
Python range() Function
>>> s = 'Python' >>> len(s) 6 >>> for i in range(len(s)): ... print(i, s[i]) ... 0 P 1 y 2 t 3 h 4 o 5 n
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Interpret "for i in n" as "for i in range(n)" if n is an int - Ideas - Discussions on Python.org
January 17, 2024 - I expect that this idea will be shot down ๐Ÿ™‚ but Iโ€™m curious to hear why. As a pure convenience / syntactic sugar thing, Iโ€™d love not having to write โ€œfor i in range(n)โ€ ever again and instead be able to say โ€œfor i in nโ€ with no ambiguity (at least when n is an integer at runtime).
๐ŸŒ
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
2
It means that you're looping through the function body n times. So, for i in range(n) means that you're going to do something n times. For example: for i in range(10): print(n) means you're going to print the numbers 0 to 9, because the range function goes from 0 to n-1 if you provide a single argument, and from a to b if you provide two, as in range(a, b)
Find elsewhere
๐ŸŒ
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
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ ForLoop
ForLoop - Python Wiki
When you have a block of code you want to run x number of times, then a block of code within that code which you want to run y number of times, you use what is known 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))
๐ŸŒ
Quora
quora.com โ€บ What-does-for-I-in-range-mean-in-Python-and-in-easy-to-understand-language-I-am-just-starting-to-learn
What does 'for I in range()' mean in Python, and in easy to understand language (I am just starting to learn)? - Quora
Answer (1 of 5): Range(โ€ฆ) is a function which generates a sequence of numbers for example : range(1,10) will generate the sequence 1,2,3,4,5.6,7,8,9 (note that the last value is never included). a โ€˜for loopโ€™ takes a sequence (such as generated by range(โ€ฆ) ),and places a value from the sequence i...
๐ŸŒ
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 - In this syntax, the range() function increases the start value by one until it reaches the stop value. The following example uses a for loop to show five numbers, from 1 to 5 to the screen: for index in range(1, 6): print(index)Code language: Python (python)
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ range
Python range() Function
# create a sequence from 0 to 3 numbers = range(4) # iterating through the sequence for i in numbers: print(i) ... The start and step arguments are optional. The range() function returns an immutable sequence of numbers. # create a sequence from 0 to 3 (4 is not included) numbers = range(4) # convert to list and print it print(list(numbers)) # Output: [0, 1, 2, 3]
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - Python for loop executes a block of code or statement repeatedly for a fixed number of times. We can iterate over a sequence of numbers produced by the range() function using for loop. Letโ€™s see how to use for loop with range() function to print the odd numbers between 1 and 10.
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ python
Understanding the 'for i in range' Statement in Python
July 5, 2023 - A: You can use the 'len()' function to get the length of the list, and then use that in the 'range()' function. For example, 'for i in range(len(my_list)):'. For more information, you can check out the official Python documentation.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ loops - for i and for j in range(n) explained
r/learnpython on Reddit: Loops - for i and for j in range(n) explained
July 3, 2021 -

I'm a Python beginner and wondering if anyone can explain what the for j in range i line is doing here? In addition, what is the proper name for these i and j expressions?

n=5;
for i in range(n):
    for j in range(i):
        print ('* ', end="")
    print('')
for i in range(n,0,-1):
    for j in range(i):
        print('* ', end="")
    print('')
๐ŸŒ
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 - ... This loop iterates over each character in the string "Hello". The for loop assigns each character ('H', 'e', 'l', 'l', 'o') in turn to the variable char and prints it. So, the output will be each character of "Hello" on a new line.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ range
Python's range() function - Python Morsels
January 14, 2025 - Imagine if we were working with 100 numbers... or 1,000 numbers! Instead, we could use one of Python's built-in functions: the range function. The range function accepts a start integer and a stop integer: >>> for n in range(1, 11): ...
๐ŸŒ
Python Central
pythoncentral.io โ€บ pythons-range-function-explained
What Is the Range of the Function | Python for Range | Range() Python
January 27, 2022 - So in Python 3.x, the range() function got its own type. In basic terms, if you want to use range() in a for loop, then you're good to go. However you can't use it purely as a list object. For example you cannot slice a range type. When you're using an iterator, every loop of the for statement produces the next number on the fly.