🌐
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,
Discussions

What will the following code output? for i in range(5):   print(i, end=' ')   a 0 1 2 3 4 b 1 2 3 4 5 c 0 1 2 3 d 1 2 3 4
Solution For What will the following code output? for i in range(5): print(i, end=' ') a 0 1 2 3 4 b 1 2 3 4 5 c 0 1 2 3 d 1 2 3 4 More on askfilo.com
🌐 askfilo.com
1
March 25, 2026
Type the program's output for i in range(0, 5): print(i * 2, 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
July 22, 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
🌐
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)
🌐
Sololearn
sololearn.com › en › Discuss › 9517 › for-i-in-range5-print-anything-what-does-i-here-depicts-
for i in range(5): print(" anything.. ") What does i here ...
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.
🌐
Filo
askfilo.com › higher education › smart solutions › what will the following code output? for i in range(5):   prin
What will the following code output? for i in range(5): print(i, end=..
March 25, 2026 - So, range(5) produces the sequence: 0,1,2,3,4. Execute the first iteration: i=0. The program prints 0 . Execute the second iteration: i=1. The program prints 1 . Execute the third iteration: i=2. The program prints 2 . Execute the fourth iteration: ...
🌐
Brainly
brainly.com › computers and technology › high school › what does the following python code print? ```python for i in range(5): print(i) ```
[FREE] What does the following Python code print? python for i in range(5): print(i) - brainly.com
November 27, 2023 - The Python code prints the numbers 0 through 4, each on a separate line. This occurs because a for loop iterates over a range of integers generated by the range function. Specifically, the range(5) produces the numbers 0 to 4 for printing.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-range-function
Python range() function - GeeksforGeeks
step (optional): Difference between consecutive numbers (default is 1) Return: A range object representing the sequence · 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 ·
Published   March 10, 2026
🌐
Studocu
studocu.com › southern new hampshire university › introduction to scripting › question
[Solved] Type the programs output for i in range0 5 printi 2 end - Introduction to Scripting (IT140) - Studocu
July 22, 2024 - Type the program's output for i in range(0, 5): print(i * 2, end=' ') Like0 · 1 year ago · The program you've provided is a simple Python loop that iterates over a range of numbers from 0 to 4 (since the range() function in Python is exclusive ...
Find elsewhere
🌐
Python Central
pythoncentral.io › pythons-range-function-explained
What Is the Range of the Function | Python for Range | Range() Python
January 27, 2022 - [python] >>> # One parameter >>> for i in range(5): ... print(i) ... 0 1 2 3 4 >>> # Two parameters >>> for i in range(3, 6): ... print(i) ... 3 4 5 >>> # Three parameters >>> for i in range(4, 10, 2): ... print(i) ... 4 6 8 >>> # Going backwards >>> for i in range(0, -10, -2): ...
🌐
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)
🌐
Sololearn
sololearn.com › en › Discuss › 56127 › how-does-this-work-why-is-there-an-ifor-i-in-range5-printhello
How does this work? why is there an i? for i in range(5): print("hello!") | Sololearn: Learn to code for FREE!
... i ... is a variable When you write >>> for i in range(10): ....... print("Hello") Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello It means that the variable called "i" will assume the value 0 then 1 then 2 ....
🌐
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 ...
Top answer
1 of 6
55

A "for loop" in most, if not all, programming languages is a mechanism to run a piece of code more than once.

This code:

for i in range(5):
    print i

can be thought of working like this:

i = 0
print i
i = 1
print i
i = 2
print i
i = 3
print i
i = 4
print i

So you see, what happens is not that i gets the value 0, 1, 2, 3, 4 at the same time, but rather sequentially.

I assume that when you say "call a, it gives only 5", you mean like this:

for i in range(5):
    a=i+1
print a

this will print the last value that a was given. Every time the loop iterates, the statement a=i+1 will overwrite the last value a had with the new value.

Code basically runs sequentially, from top to bottom, and a for loop is a way to make the code go back and something again, with a different value for one of the variables.

I hope this answered your question.

2 of 6
25

When I'm teaching someone programming (just about any language) I introduce for loops with terminology similar to this code example:

for eachItem in someList:
    doSomething(eachItem)

... which, conveniently enough, is syntactically valid Python code.

The Python range() function simply returns or generates a list of integers from some lower bound (zero, by default) up to (but not including) some upper bound, possibly in increments (steps) of some other number (one, by default).

So range(5) returns (or possibly generates) a sequence: 0, 1, 2, 3, 4 (up to but not including the upper bound).

A call to range(2,10) would return: 2, 3, 4, 5, 6, 7, 8, 9

A call to range(2,12,3) would return: 2, 5, 8, 11

Notice that I said, a couple times, that Python's range() function returns or generates a sequence. This is a relatively advanced distinction which usually won't be an issue for a novice. In older versions of Python range() built a list (allocated memory for it and populated with with values) and returned a reference to that list. This could be inefficient for large ranges which might consume quite a bit of memory and for some situations where you might want to iterate over some potentially large range of numbers but were likely to "break" out of the loop early (after finding some particular item in which you were interested, for example).

Python supports more efficient ways of implementing the same semantics (of doing the same thing) through a programming construct called a generator. Instead of allocating and populating the entire list and return it as a static data structure, Python can instantiate an object with the requisite information (upper and lower bounds and step/increment value) ... and return a reference to that.

The (code) object then keeps track of which number it returned most recently and computes the new values until it hits the upper bound (and which point it signals the end of the sequence to the caller using an exception called "StopIteration"). This technique (computing values dynamically rather than all at once, up-front) is referred to as "lazy evaluation."

Other constructs in the language (such as those underlying the for loop) can then work with that object (iterate through it) as though it were a list.

For most cases you don't have to know whether your version of Python is using the old implementation of range() or the newer one based on generators. You can just use it and be happy.

If you're working with ranges of millions of items, or creating thousands of different ranges of thousands each, then you might notice a performance penalty for using range() on an old version of Python. In such cases you could re-think your design and use while loops, or create objects which implement the "lazy evaluation" semantics of a generator, or use the xrange() version of range() if your version of Python includes it, or the range() function from a version of Python that uses the generators implicitly.

Concepts such as generators, and more general forms of lazy evaluation, permeate Python programming as you go beyond the basics. They are usually things you don't have to know for simple programming tasks but which become significant as you try to work with larger data sets or within tighter constraints (time/performance or memory bounds, for example).

[Update: for Python3 (the currently maintained versions of Python) the range() function always returns the dynamic, "lazy evaluation" iterator; the older versions of Python (2.x) which returned a statically allocated list of integers are now officially obsolete (after years of having been deprecated)].

🌐
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 - 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
🌐
Sololearn
sololearn.com › en › Discuss › 9517 › for-i-in-range-5-print-anything-what-does-i-here-depicts
for i in range(5): print(" anything.. ") What does i here depicts?? | Sololearn: Learn to code for FREE!
June 26, 2016 - i represents the number of values in the list range(5), because range produces a list. range(5)==[0, 1, 2, 3, 4] if it were: poo=['blah', 'fork', 'winslow'] for i in poo: print('jackalope!') it would result in: >>> jackalope!
🌐
Brainly
brainly.in › computer science › secondary school
for i in range(5,0,-1): print(i) - Brainly.in
June 20, 2024 - -1 tells that this is a descending loop starting from 5 till 0(not included) and the next line prints them.. Your client is developing a project in IntelliJ IDEA and is seeking to implement a coding agent to enhance productivity. Which coding agent would be t … he most appropriate recommendation for their IntelliJ-based workflow?