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
result = 0 n = 5 for i in range(1, n + 1): result += i # this ^^ is the shorthand for # result = result + i print(result)
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
Which of the following Python code will give different output from the others? a. for i in range (0,5): print (i) c. N= [0,1,2,3,4,5] for k in N: print (k) b. N= [0,1,2,3,4] for j in N: print (j) d. for 1 in range (0,5,1): B. Find error in the following code fragments. for j in (10): print (j) for i in range [1; 10]: print (i) print (1) while x != 3 print x C. Give the output for the following code fragments. for num in range (2,-5,-1): print (num) for i in "John": print (i) a = 100 while a > 90: print (a) a = a- 2 D. Answer the following questions. i = 0 0 sum = while i < 9: if i % 4 == 0: sum = sum + i i = i + 2 print (sum) What is the purpose of range() function? Give an example. Differentiate between for and while loop. What is an infinite loop? Give an example. #OpenForum A discussion on the 'Importance of Looping Constructs in Programming' can be take up in the class. 102 Lab 3
Solution For Which of the following Python code will give different output from the others? a. for i in range (0,5): print (i) c. N= [0,1,2,3,4,5] for More on askfilo.com
🌐 askfilo.com
1
January 8, 2025
🌐
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)
🌐
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.
🌐
Filo
askfilo.com › cbse › smart solutions › for i in range(5, 0, -1): for j in range(1, i + 1): print(j, e
python for i in range(5, 0, -1): for j in range(1, i + 1): ... | Filo
December 22, 2025 - 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 ...
Find elsewhere
🌐
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.
🌐
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
🌐
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 ...
🌐
Filo
askfilo.com › cbse › smart solutions › which of the following python code will give different output
Which of the following Python code will give different output from the ot..
January 8, 2025 - 1. for j in (10): print (j) 3. for i in range [1; 10]: print (i) print (1) 2. while x != 3 print x C. Give the output for the following code fragments. 1. for num in range (2,-5,-1): print (num) 2. for i in "John": print (i) 3. a = 100 while ...
🌐
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...
🌐
CodeHS
codehs.com › textbook › intropython_textbook › 3.2
3.2 For Loops
3.1 While Loops · 3.2 For Loops · For Loops · Print Ten Hellos · The Range Function · Counting in Range · One More Range Parameter · Running Total · Running Total, Part 2 · Check Your Understanding · Exercise: Average Receipt Value · 3.3 Break and Continue · 3.4 Nested Control Structures · 4. Functions and Exceptions · 5.
🌐
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]) ......
🌐
W3Schools
w3schools.com › python › ref_func_range.asp
Python range() Function
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 » · 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.
🌐
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. ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-for-loops
Python For Loops - GeeksforGeeks
Here, for loop is used to iterate through each character of the string and prints them one by one. ... range() function is used with for loops to generate a sequence of numbers. It can take one, two or three arguments: ... These statements are ...
Published   May 8, 2026