🌐
Snakify
snakify.org › for loop with range
For loop with range - Learn Python 3 - Snakify
For instance, any string in Python is a sequence of its characters, so we can iterate over them using for: ... Another use case for a for-loop is to iterate some integer variable in increasing or decreasing order. Such a sequence of integer can be created using the function range(min_value, max_value): ... Function range(min_value, max_value) generates a sequence with numbers min_value, min_value + 1, ..., max_value - 1.
Discussions

python - why for i in range(0,10,-1): wont print anything - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
python - Decrementing for loops - Stack Overflow
I want to have a for loop like so: for counter in range(10,0): print counter, and the output should be 10 9 8 7 6 5 4 3 2 1 More on stackoverflow.com
🌐 stackoverflow.com
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: 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
🌐
PYnative
pynative.com › home › python › python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - # 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 (python) Run
🌐
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 - If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive. ... Copied!# 👇️ for loop 1 to 10 (including 10) for num in range(1, 11): print(num) # 👇️ [1, 2, 3, 4, 5, 6, ...
🌐
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
🌐
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 for loop is used to iterate through the range of numbers from 1 to 10 (inclusive). 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( ...
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() Function
>>> s = 'Python' >>> len(s) 6 >>> for i in reversed(range(len(s))): ... print(i, s[i]) ... 5 n 4 o 3 h 2 t 1 y 0 P · Range with 2 parameters specifies a start number other than 0, but is otherwise like the 1 parameter form above, going up to but not including the stop number. >>> list(range(5, 10)) [5, 6, 7, 8, 9] >>> list(range(5, 7)) [5, 6] >>> list(range(5, 6)) [5] >>> list(range(5, 5)) # start >= stop, no numbers [] >>> list(range(0, 5)) [0, 1, 2, 3, 4] >>> list(range(0, 0)) [] >>> list(range(0, -1)) []
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop - Python Wiki
def my_range(start, end, step): while start <= end: yield start start += step for x in my_range(1, 10, 0.5): print(x)
🌐
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....
🌐
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 ...
🌐
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 Morsels
pythonmorsels.com › range
Python's range() function - Python Morsels
January 14, 2025 - Trey Hunner 3 min. read • 2 min. video • Python 3.10—3.14 • Jan. 14, 2025 ... Sign in to your Python Morsels account to save your screencast settings. Don't have an account yet? Sign up here. Let's talk about Python's range function. ... >>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> for n in numbers: ...
🌐
Quora
quora.com › What-is-a-short-Python-code-to-print-numbers-from-10-to-1
What is a short Python code to print numbers from 10 to 1? - Quora
Answer (1 of 16): If you want output in line use: [code ]print(range(10,0,-1))[/code] And else you can use loop [code]for number in range(10, 0, -1): print(number) [/code]
🌐
Learn Python
learnpython.org › en › Loops
Loops - Learn Python - Free Interactive Python Tutorial
# Prints out 0,1,2,3,4 and then it prints "count value reached 5" count=0 while(count<5): print(count) count +=1 else: print("count value reached %d" %(count)) # Prints out 1,2,3,4 for i in range(1, 10): if(i%5==0): break print(i) else: print("this ...
🌐
Mimo
mimo.org › glossary › python › range-function
Mimo: The coding platform you need to learn Web Development, Python, and more.
To add together two ranges, you need to convert the sequences into lists beforehand. ... range1 = range(5) range2 = range(5, 10) # Convert to lists and concatenate combined_list = list(range1) + list(range2) print(combined_list) # Outputs: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
🌐
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...
🌐
Codedamn
codedamn.com › news › python
Understanding the 'for i in range' Statement in Python
July 5, 2023 - For example, 'for i in range(10, 0, -1):' would count down from 10 to 1. Q: How can I iterate over a list using the 'for i in range' statement? A: You can use the 'len()' function to get the length of the list, and then use that in the 'range()' ...