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. Answer from Sadapy on reddit.com
🌐
freeCodeCamp
freecodecamp.org › news › python-for-loop-for-i-in-range-example
Python For Loop - For i in Range Example
March 30, 2021 - # Example with one argument for i in range(5): print(i, end=", ") # prints: 0, 1, 2, 3, 4,
🌐
Snakify
snakify.org › for loop with range
For loop with range - Learn Python 3 - Snakify
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: ... By default, the function print() prints all its arguments separating them by a space and the puts a newline symbol after it. This behavior can be changed using keyword arguments sep (separator) and end. ... print(1, 2, 3) print(4, 5, 6) print(1, 2, 3, sep=', ', end='. ') print(4, 5, 6, sep=', ', end='. ') print() print(1, 2, 3, sep='', end=' -- ') print(4, 5, 6, sep=' * ', end='.')
Discussions

for i in range(5): print(i, end=' ')
On Studocu you find all the lecture notes, summaries and study guides you need to pass your exams with better grades. More on studocu.com
🌐 studocu.com
1
November 21, 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
14
17
September 9, 2019
python for i in range(5, 0, -1): for j in range(1, i + 1): print(j, end="") print()
The code uses nested for loops to print numbers in a pattern. The outer loop runs from 5 down to 1 (inclusive), decrementing by 1 each time. The inner loop runs from 1 up to the current value of the outer loop variable i. For each iteration of the inner loop, it prints the current number j ... More on askfilo.com
🌐 askfilo.com
1
December 22, 2025
python range and for loop understanding - Stack Overflow
So I know that the range function takes in 3 parameters: range(start, stop, step). For the below function, I passed in my array called test_numbers, then created a "for" loop where "i" starts counting from len(list_of_numbers)-1 (to give me the index values) I was expecting the result for "i" to print 0,1,2,3,4,5 ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python Examples
pythonexamples.org › python-for-i-in-range
Python for i in range() - Python Examples
In this example, we will take a ... 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....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-range-function
Python range() function - GeeksforGeeks
for i in range(5): print(i, end=" ") Output · 0 1 2 3 4 · Explanation: range(5) generates numbers from 0 to 4 · i takes one value at a time from the generated sequence ·
Published   March 10, 2026
🌐
Studocu
studocu.com › southern new hampshire university › introduction to scripting › question
[Solved] for i in range5 printi end - Introduction to Scripting (IT140) - Studocu
November 21, 2024 - In this case, range(5) generates a sequence of numbers from 0 to 4. print(i, end=' ') This is the Python print function. It prints the value of i for each iteration of the loop. The end=' ' argument changes the default behavior of the print function.
🌐
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)
🌐
PYnative
pynative.com › home › python › python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - # reverse range using negative step # start = 5 # stop = -1 # step = -1 for i in range(5, -1, -1): print(i)Code language: Python (python) Run
Find elsewhere
🌐
Learn Python
learnpython.org › en › Loops
Loops - Learn Python - Free Interactive Python Tutorial
# Prints out 0,1,2,3,4 and then ... for i in range(1, 10): if(i%5==0): break print(i) else: print("this is not printed because for loop is terminated because of break but not due to fail in condition")...
🌐
Python Central
pythoncentral.io › pythons-range-function-explained
What Is the Range of the Function | Python for Range | Range() Python
January 27, 2022 - The syntax to access the first element of a list is mylist[0]. Therefore the last integer generated by range() is up to, but not including, stop. For example range(0, 5) generates integers from 0 up to, but not including, 5. [python] >>> # One parameter >>> for i in range(5): ... print(i) ...
🌐
Mimo
mimo.org › glossary › python › range-function
Python range() Function [Python Tutorial]
for i in range(10, 0, -1): print(i) # Outputs: 10 9 8 7 6 5 4 3 2 1 · A machine learning application platform might use the range() function to process large training datasets in chunks of 100. By batch-processing data, the application could minimize memory usage and improve performance.
🌐
Sololearn
sololearn.com › en › Discuss › 1856560 › for-i-in-range-1-5-1-print
for i in range(1,5,-1): print("$$")
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
PyTutorial
pytutorial.com › python-for-i-in-range-loop-tutorial
PyTutorial | Python For i in Range Loop Tutorial
March 28, 2026 - # Basic loop that runs 5 times (0 to 4) for i in range(5): print(f"Iteration number: {i}") Iteration number: 0 Iteration number: 1 Iteration number: 2 Iteration number: 3 Iteration number: 4 · In this example, i takes on each value from the ...
🌐
Python Guides
pythonguides.com › for-i-in-range-python
Understand for i in range Loop in Python
October 7, 2025 - # Generate a list of squares from ... generates numbers from 0 to n-1, not n: # Incorrect if you want numbers 1 to 5 for i in range(5): print(i) # Prints 0, 1, 2, 3, 4 # Correct if you want numbers 1 to 5 for i in range(1, 6): print(i) # Prints 1, 2, 3, 4, 5...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-loop-through-a-range
Python - Loop Through a Range - GeeksforGeeks
March 27, 2026 - import numpy as np # Loop through floating-point numbers for i in np.arange(0.5, 2.5, 0.5): print(i, end=" ") ... Here, NumPy allows iteration over fractional ranges. It Supports precise step values for scientific or mathematical applications. ...
🌐
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”.
🌐
StrataScratch
stratascratch.com › blog › python-for-loop-range-function
How Does Python For Loop Range Function Work? - StrataScratch
November 5, 2025 - Notice that it starts at zero and stops before 5. This is Python's way of counting, zero-indexing. You can use ranges in three different ways by combining start, stop, and step arguments. ... Let’s write a range that stops at 8. ... Here is the output. Now, let’s start the numbers from 3 by adding the start parameter to the range function. ... Here is the output. Let’s find odd numbers from 1 to 10.