Showing results for for i in range python
Search instead for for i j in range python

When you call range() with two arguments, the first argument is the starting number, and the second argument is the end (non-inclusive). So you're starting from len(list_of_numbers), which is 5, and you're ending at 6. So it just prints 5.

To get the results you want, the starting number should be 0, and the end should be len(list_of_numbers)+1. If you call it with one argument, that's the end, and 0 is the default start. So use

for i in range(len(list_of_numbers)+1):

or if you want to pass the start explicitly:

for i in range(0, len(list_of_numbers)+1):
Answer from Barmar on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_range.asp
Python range() Function
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. ... Coding fundamentals as bite-sized lessons and challenges.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-for-loop-for-i-in-range-example
Python For Loop - For i in Range Example
March 30, 2021 - If range() is called with only one argument, then Python assumes start = 0. The stop argument is the upper bound of the range. It is important to realize that this upper value is not included in the range. In the example below, we have a range starting at the default value of 0 and including integers less than 5. # Example with one argument for i in range(5): print(i, end=", ") # prints: 0, 1, 2, 3, 4,
Discussions

python range and for loop understanding - Stack Overflow
I am new to python / coding and looking to understanding the range function more and how it is used in conjunction with the "for" loop. So I know that the range function takes in 3 parame... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Understanding range() in a for loop - Stack Overflow
I was just wandering why this program does not work if I modify my for loop from for i in range(len(string[i]) with for i in string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
i in Range, what is it and how does it come to be?
The syntax is structured like this: for variable in iterable: iterable is something that you can iterate over, a list, or a range, etc... So each cycle variable gets one item from iterable... In this case the iterable is range(0,9) object. More on reddit.com
๐ŸŒ r/learnpython
6
16
October 31, 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
15
18
September 9, 2019
๐ŸŒ
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
3
range is a function that returns a list of numbers (in the range passed as argument) So when you write "for i in range(x)" you mean: "for each ITEM in LIST: do something" For each item in the list passed after the IN operator, code will run the next codes you wrote. And inside the loop, the current item is identifyed as "I" Like: my_list = ["Nice","Code","Dude","!"] for item in my_list: print(item) In other words: for in : actions for each item
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-range-function
Python range() function - GeeksforGeeks
The range() function in Python is used to generate a sequence of integers within a specified range.
Published: March 10, 2026
๐ŸŒ
Stanford
web.stanford.edu โ€บ class โ€บ archive โ€บ cs โ€บ cs106a โ€บ cs106a.1204 โ€บ handouts โ€บ py-range.html
Python range() Function
The python range() function creates a collection of numbers on the fly, like 0, 1, 2, 3, 4. This is very useful, since the numbers can be used to index into collections such as string. The range() function can be called in a few different ways. The most common form is range(n), for integer n, which returns a numeric series starting with 0 and extending up to but not including n, e.g.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ ForLoop
For Loop - Python Wiki
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))
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ mrmattperreault
#Python Crash Course - Iteration, For Loops and Range Function - YouTube
#Python Crash Course - In this video we explore how for loops work. We start by iterating through a list of numbers, then iterate through a string. Next we ...
Published: December 13, 2022
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ range
Python's range() function - Python Morsels
July 18, 2026 - 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) ... 1 2 3 4 5 6 7 8 9 10 ยท The range function counts upward starting from that start number, and it stops just before that stop number.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-range.html
Python range() - Stanford Computer Science
Mnemonic: the "stop" number is strong, so as soon as the numbers hit or exceed the stop the range is done (even if the start number initiates things in that position). The 3 parameter form begins with start number, up to but no including the stop number as usual.
๐ŸŒ
Real Python
realpython.com โ€บ python-range
Python range(): Represent Numerical Ranges โ€“ Real Python
1 week ago - In Python, the range() function generates a sequence of numbers, often used in loops for iteration. By default, it creates numbers starting from 0 up to but not including a specified stop value. You can also reverse the sequence with reversed().
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ python-for-loop-range-function
How Does Python For Loop Range Function Work? - StrataScratch
November 5, 2025 - Here is how to implement a countdown with Python by using a for loop: import time print("Starting countdown...") for i in range(3, 0, -1): print(f"Please wait...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - The step value must not be zero. If a step=0, Python will raise a ValueError exception. ... Use range() to generate a sequence of numbers starting from 9 to 100 divisible by 3. ... # start = 9 # stop = 100 # step = 3 (increment) for i in range(9, 100, 3): print(i) Code language: Python (python) Run
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python For Loop with range() on Lists | Iterate Using Index in Python - YouTube
In this Python tutorial, youโ€™ll learn how to use a for loop with range() on lists. Unlike looping directly through list items, this method gives you access t...
Published: August 17, 2025
Views: 13
๐ŸŒ
Snakify
snakify.org โ€บ for loop with range
For loop with range - Learn Python 3 - Snakify
Same as with if-else, indentation is what specifies which instructions are controlled by for and which aren't. Range() can define an empty sequence, like range(-5) or range(7, 3).
๐ŸŒ
Medium
medium.com โ€บ @tamarainwang โ€บ when-to-use-for-i-in-range-vs-a68a6f32da9f
When to Use for i in range () for loop vs. for item in iterable () for loop in Python: A Guide with Creative Employee Benefits | by Tamara Inwang | Medium
July 27, 2024 - If you call range() with only one argument, Python assumes start is 0. The stop argument is the upper limit of the range, but this value is not included in the range. The step argument determines how many places to move forward (or backward ) between each value in the range.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ range-function
Python range(): start, stop and step in Loops
Within for loops in Python, range() can control the number of iterations of the loop.
๐ŸŒ
YouTube
youtube.com โ€บ neso academy
for Loop with range() Function in Python - YouTube
Python Programming: for Loop with range() Function in PythonTopics discussed:1. Introduction to range() Function.2. for Loop with range(stop).3. for Loop wit...
Published: October 2, 2023
Views: 49K