🌐
GeeksforGeeks
geeksforgeeks.org › python › python-range-function
Python range() function - GeeksforGeeks
Numbers increase by the default ... 10, 2): print(v, end=" ") Output · 0 2 4 6 8 · Explanation: range(0, 10, 2) increases the value by 2 each time ·...
Published   March 10, 2026
🌐
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....
Discussions

Please help me understand the "range" function
Lets say you have range(s, e), it will generate a sequence of numbers from s to e - 1, so if you do range(1, 100) it will be from 1 to 99. If you omit the first argument then python will use 0 as the default value, so in your example range is basically being called as range(0, 2) which will generate the sequence [0, 1] You can also pass a value to the step parameter to dictate how values of the range are incremented, the default value is 1. edit: You can also pass a negative value to step and it will generate a reverse sequence. So if you call range(10, 0, -2) it will return the sequence [10, 8, 6, 4, 2] More on reddit.com
🌐 r/learnpython
22
8
March 26, 2026
python - How do I use a decimal step value for range()? - Stack Overflow
It's embarrassing that python's range dosen't allow this, given how easy it is to implement a generator that does this even without accumulating rounding errors. Heck, even the seq tool in GNU coreutils allows one to do seq 0 0.1 1 without rounding errors! ... @josch: seq uses the C long double type internally, and is subject to rounding errors. For example on my machine, seq 0 0.1 1 gives 1 as its last output (as expected), but seq 1 0.1 2 ... More on stackoverflow.com
🌐 stackoverflow.com
Understanding Range() function in python. [Level: Absolute Beginner]
You can think of range as returning a sequence of numbers. Since we use the syntax for in : to loop or iterate over a sequence, naturally we can put a range as the sequence. Does that help? More on reddit.com
🌐 r/learnpython
23
4
August 6, 2024
How do while not loops work
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa. More on reddit.com
🌐 r/learnpython
16
5
September 11, 2024
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() Function
The most common form is range(n), given integer n returns a numeric series starting with 0 and extending up to but not including n, e.g. range(6) returns 0, 1, 2, 3, 4, 5. With Python's zero-based indexing, the contents of a string length 6, are at index numbers 0..5, so range(6) will produce ...
🌐
PYnative
pynative.com › home › python › python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - The range() by default starts at 0, not 1, if the start argument is not specified. For example, range(5) will return 0, 1, 2, 3, 4. ... The range() function returns an object of class range, which is nothing but a series of integer numbers.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use range() in Python | note.nkmk.me
August 18, 2023 - If you want to convert a range ... and using list() is not required when working with a for loop. range(stop) generates a series of integers 0 <= i < stop....
🌐
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 ...
🌐
Vista Academy
thevistaacademy.com › home › blog › python range function in hindi — 20 examples & exercises (2026)
Python Range Function in Hindi — 20 Examples & Exercises (2026)
Tip: range() works with integers; generate 0–10 and divide for clean decimals (avoids float precision issues). ... These Python range() examples cover start, stop, and step usage, negative ranges, list conversion, and reverse iteration—matching common queries like “range function in python”, “python range examples”, “use of range function in python”.
Published   March 22, 2026
🌐
Reddit
reddit.com › r/learnpython › please help me understand the "range" function
r/learnpython on Reddit: Please help me understand the "range" function
March 26, 2026 -

EDIT: Holy cow guys, I didn't expect such a helpful community! Thanks a lot everyone, I think I get it now. Cheers!

Hi! I've been enjoying learning python through the Py4e course. However, I can't wrap my head around the range function, specifically in the section about lists. They suggest using it to traverse lists like that:

numbers = [17, 123]
for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2
    print (numbers[i])

But I'm too dumb to understand what this does. Tried reading about it in the python library, only got more confused.

  1. Could someone please spell out to me the logic behind this piece of code? What exactly does range do there, and why does it enables you to use square brackets to select elements of the list?

  2. Why do you need to combine it with the "len" function here to get this result?

  3. Why does "print (range(len(numbers)))" return "range(0, 3)" and not "(0, 3)" or "range(0, 1, 2, 3)"?

  4. The code below seems to give the same result as the earlier one, so what's the difference?for i in numbers: print (i*2)

Top answer
1 of 5
6
Lets say you have range(s, e), it will generate a sequence of numbers from s to e - 1, so if you do range(1, 100) it will be from 1 to 99. If you omit the first argument then python will use 0 as the default value, so in your example range is basically being called as range(0, 2) which will generate the sequence [0, 1] You can also pass a value to the step parameter to dictate how values of the range are incremented, the default value is 1. edit: You can also pass a negative value to step and it will generate a reverse sequence. So if you call range(10, 0, -2) it will return the sequence [10, 8, 6, 4, 2]
2 of 5
5
The other answers are already quite decent, but I'll see if I can slightly simplify the explanations further. Could someone please spell out to me the logic behind this piece of code? What exactly does range do there, and why does it enables you to use square brackets to select elements of the list? Let's start by explaining what the program does. numbers = [17, 123] for i in range(len(numbers)): numbers[i] = numbers[i] * 2 print (numbers[i]) You have a list of integers. In the loop, the program first doubles the value of each number in the list, and replaces the original values with the doubled values, before printing out the now-doubled value. In order to replace a value in a list, you need to know what index it's in. That's more or less why range is being used here; you get a collection of integers that neatly map into the indices of the list. For example, in numbers, 17 is located at index 0 and 123 at indexd 1. len(numbers) evaluates to 2, so range(len(numbers)) is equivalent to writing range(2). And range(2) in turn returns a collection of integers, which we can think of as a list like [0, 1]. range technically returns a range object, not a list, but for this scenario there's no practical difference. With the for-loop, you then get these indices as values for i. If you want to "unroll" the loop, it would then be equivalent to doing numbers[0] = numbers[0] * 2 print(numbers[0]) numbers[1] = numbers[1] * 2 print(numbers[1]) but you don't need to repeat all that yourself, and it doesn't matter how long numbers actually is! Why do you need to combine it with the "len" function here to get this result? Technically speaking you don't, if you hardcode the length of the list, but this way you don't need to update the arguments to range if you add or remove numbers from numbers. Why does "print (range(len(numbers)))" return "range(0, 3)" and not "(0, 3)" or "range(0, 1, 2, 3)"? As I explained earlier, range is its own type. An in-depth explanation would probably fly right over your head right now, but in a nutshell, unlike a list it doesn't need to store every individual number in memory all at once. It can calculate them dynamically as your loop requests them, saving memory. The code below seems to give the same result as the earlier one, so what's the difference? for i in numbers: print (i*2) I explained this earlier, too, but in this case you don't modify the list at all and simply print out the doubled numbers. With all that said, more experienced developers would not use range for this, but enumerate: numbers = [17, 123] for index, num in enumerate(numbers): numbers[index] = num * 2 print(numbers[index]) In this case, we get the index basically for free, while still getting the actual number in the original list, so the end result is a bit cleaner. That being said, you could technically also use numbers = [17, 123] for index, _ in enumerate(numbers): numbers[index] *= 2 print(numbers[index]) so it's not like there aren't options.
Find elsewhere
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - If you need any special properties of the reversed range object, then you can use [::-1] or a similar calculation instead. ... So far, you’ve used integers when setting up ranges. You can also use integer-like numbers like binary numbers or hexadecimal numbers instead: ... Here, 0b110 is the binary representation of 6, while 0xeb is the hexadecimal representation of 235.
🌐
Runestone Academy
runestone.academy › ns › books › published › thinkcspy › PythonTurtle › TherangeFunction.html
4.7. The range Function — How to Think like a Computer Scientist: Interactive Edition
Even though you can use any four ... range objects that can deliver a sequence of values to the for loop. When called with one parameter, the sequence provided by range always starts with 0. If you ask for range(4), then you will get 4 values starting with 0. In other ...
🌐
Python Central
pythoncentral.io › pythons-range-function-explained
What Is the Range of the Function | Python for Range | Range() Python
January 27, 2022 - The range() function works a little ... parameters, as follows: ... stop: Number of integers (whole numbers) to generate, starting from zero. eg. range(3) == [0, 1, 2]....
🌐
Tutorial Gateway
tutorialgateway.org › python-range-function
Python range Function
March 31, 2025 - If you omit this, the Python range function will start from 0. Stop – A value before this number is the end value. For example, (1, 10) print values from 1 to 9. Step (optional) – Sequence of numbers generated. It determines the space or difference between each integer value. For example, (1, 10, 2...
🌐
w3resource
w3resource.com › python › built-in-function › range.php
Python range() function - w3resource
April 18, 2026 - Returns an immutable sequence object of integers. ... # empty range print(list(range(0))) # using range(stop) print(list(range(12))) # using range(start, stop) print(list(range(1, 15))) ... # range upto 10 print(range(10)) # range upto 30 but 5 step jumps print(range(0, 30, 5)) # range() works with negative print(range(0, -10, -1)) Example-3: Python range() works with negative step
🌐
Programiz
programiz.com › python-programming › methods › built-in › range
Python range() Function
# create a sequence from 2 to 4 (5 is not included) numbers = range(2, 5) print(list(numbers)) # [2, 3, 4] # create a sequence from -2 to 3 numbers = range(-2, 4) print(list(numbers)) # [-2, -1, 0, 1, 2, 3] # creates an empty sequence numbers = range(4, 2) print(list(numbers)) # []
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › cs › csse120 › VideoFiles › 08.1-RangeExpressions › RangeExpressions.pdf pdf
Python’s range expression Recall that a range expression
Python’s range expression · Recall that a range expression · generates integers that can be · used in a FOR loop, like this: In that example, k takes on the · values 0, 1, 2, ... n-1, as the loop runs. That is: Python allows two other forms of the range expression, for your ·
🌐
Finxter
blog.finxter.com › home › learn python blog › python range() function — a helpful illustrated guide
Python range() Function - A Helpful Illustrated Guide - Be on the Right Side of Change
December 12, 2020 - Or you can pass start, stop, and step arguments in which case the range object will go from start to step using the given step size. For example, range(3) results in 0, 1, 2 and range(2, 7, 2) results in 2, 4, 6.
🌐
Python Morsels
pythonmorsels.com › range
Python's range() function - Python Morsels
January 14, 2025 - The range function accepts a start ... So we're stopping at 10 here instead of going all the way to 11. ... When range is given one argument, it starts at 0......
🌐
Runestone Academy
runestone.academy › ns › books › published › httlacs › python-turtle_the-range-function.html
4.7 The range Function
Even though you can use any four ... range objects that can deliver a sequence of values to the for loop. When called with one parameter, the sequence provided by range always starts with 0. If you ask for range(4), then you will get 4 values starting with 0. In other ...
🌐
TechBeamers
techbeamers.com › python-range-function
Python Range Function - TechBeamers
November 30, 2025 - Python range is one of the built-in functions available in Python. It generates a series of integers starting from a start value to a stop value as specified by the user. We can use it with a for loop…