With a negative "step", python keeps on yielding1 elements while the current value is greater than end. In this case, you start at 0. 0 is not greater than or equal to 10 so python's done and nothing gets yielded.


1This is a simplification of course -- range returns a range object on python3.x which is an indexable sequence type so it doesn't exactly yield, but the basic idea is the same ...

Answer from mgilson 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 - 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,
🌐
Snakify
snakify.org β€Ί for loop with range
For loop with range - Learn Python 3 - Snakify
Let's have more complex example and sum the integers from 1 to n inclusively. ... result = 0 n = 5 for i in range(1, n + 1): result += i # this ^^ is the shorthand for # result = result + i print(result)
Discussions

python - why for i in range(0,10,-1): wont print anything - Stack Overflow
Why the above program prints nothing ... i in range(start, end, iterator)" definition ,it evaluates first element and then uses iterator to get to next element. So in theory the Example code snippet should first take 0 and print it and then next element is evaluated as -1 which is not in 0-10 then it should ... More on stackoverflow.com
🌐 stackoverflow.com
Python: for i in range (1, 10), print (i). Why does it not print the number 10?
First arg is including. Srcond one is excluding More on reddit.com
🌐 r/eli5_programming
6
3
October 29, 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
14
17
September 9, 2019
python - Print range of numbers on same line - Stack Overflow
Can you please explain how does it work, What is that * before range. 2020-07-02T04:50:24.197Z+00:00 ... @VijenderKumar This will print 1 to 10, separated by whatever string you put in the sep attribute. In the example above, each integer string is separated by a space. More on stackoverflow.com
🌐 stackoverflow.com
🌐
PYnative
pynative.com β€Ί home β€Ί python β€Ί python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - When you pass only one argument to the range(), it will generate a sequence of integers starting from 0 to stop -1. # Print first 10 numbers # stop = 10 for i in range(10): print(i, end=' ') # Output 0 1 2 3 4 5 6 7 8 9Code language: Python ...
🌐
Bobby Hadz
bobbyhadz.com β€Ί blog β€Ί python-for-loop-1-to-10
For or While loop to print Numbers from 1 to 10 in Python | bobbyhadz
April 9, 2024 - Use the `range()` class to loop from 1 to 10 in a `for` loop, e.g. `for num in range(1, 11):`.
🌐
Django Central
djangocentral.com β€Ί python-program-to-print-numbers-from-1-to-10-using-for-loop
Python Program To Print Numbers From 1 to 10 Using For Loop
The range() function generates a sequence of numbers, starting from the first argument (1) up to, but not including, the second argument (11), similar to list indexing in range starts from 0 which means range( j )will print sequence till ( j-1) hence the output doesn’t include 11.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-range-function
Python range() function - GeeksforGeeks
Example 1: This example generates numbers starting from a custom value and ending before another value. Python Β· for n in range(5, 10): print(n, end=" ") Output Β· 5 6 7 8 9 Β· Explanation: range(5, 10) starts at 5 and stops before 10 Β· Numbers increase by the default step of 1 Β·
Published Β  March 10, 2026
Find elsewhere
🌐
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 - For example, to generate a multiplication table for 5, you could: Use a for loop to iterate through numbers 1 to 10. For each iteration, multiply 5 by the current number. Print the result in the format β€œ5 x i = result”.
🌐
Mimo
mimo.org β€Ί glossary β€Ί python β€Ί range-function
Python range() Function [Python Tutorial]
With two arguments, the first is the start argument, and the second is the stop argument. ... for i in range(10): print(i) # Outputs: 0 1 2 3 4 5 6 7 8 9 for i in range(1, 10): print(i) # Outputs: 1 2 3 4 5 6 7 8 9 for i in range(1, 10, 2): print(i) # Outputs: 1 3 5 7 9
🌐
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)
🌐
Python Examples
pythonexamples.org β€Ί python-for-i-in-range
Python for i in range() - Python Examples
for i in range(5): print(i) 0 1 2 3 4 Β· In this example, we will take a range from x until y, including x but not including y, insteps of one, and iterate for each of the element in this range using For loop. for i in range(5, 10): print(i) 5 6 7 8 9 Β· In this example, we will take a range ...
🌐
CodeBasics
code-basics.com β€Ί programming β€Ί python course β€Ί for loop and range function
CodeBasics | For loop and range function | Python
for i in range(3, 0, -1): print(i) # => 3 # => 2 # => 1Expand code Β· In the examples above, we can see that the iteration completes to a final value
🌐
Python Morsels
pythonmorsels.com β€Ί range
Python's range() function - Python Morsels
January 14, 2025 - 1 2 3 4 5 6 7 8 9 10 Β· But that could get pretty tedious. 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): ... print(n) ...
🌐
Stanford CS
cs.stanford.edu β€Ί people β€Ί nick β€Ί py β€Ί python-range.html
Python range() Function
The most common form is range(n), ... index numbers for a string length 6, like this: >>> s = 'Python' >>> len(s) 6 >>> for i in range(len(s)): ... print(i, s[i]) ......