🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-range.html
Python range() Function
The python range(n) function creates a collection of numbers on the fly, like 0, 1, 2, 3 .. n-1. The numbers extend up to, but not including the n, UBNI. The numbers produced by range() are perfect for indexing into collections likes strings and lists.
🌐
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

python - meaning of the range(row,-1,-1) - Stack Overflow
0 How to understand this sentence about range, from the book Introduction to Computation and Programming Using Python · 2 What do the numbers mean in the function range(len(sequence)-1,0,-1) mean? More on stackoverflow.com
🌐 stackoverflow.com
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
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
Why is range(5) giving me range(0,5) instead of 0 1 2 3 4

Because that's how it's represented. It doesn't actually store all those values, only start, stop, and step.

More on reddit.com
🌐 r/learnpython
28
0
October 25, 2022
🌐
W3Schools
w3schools.com › python › python_range.asp
Python range
This set of numbers has its own data type called range. Note: Immutable means that it cannot be modified after it is created. The range() function can be called with 1, 2, or 3 arguments, ...
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use range() in Python | note.nkmk.me
August 18, 2023 - The arguments for np.arange() are the same as for range(), with the added support for float values. import numpy as np print(np.arange(3)) # [0 1 2] print(np.arange(3, 10)) # [3 4 5 6 7 8 9] print(np.arange(3, 10, 2)) # [3 5 7 9] print(np.arange(0.3, 1.0, 0.2)) # [0.3 0.5 0.7 0.9] ... See the following articles for np.arange() and conversion between numpy.ndarray and list. numpy.arange(), linspace(): Generate ndarray with evenly spaced values ... Python2 includes two functions: range() and xrange().
🌐
Python Morsels
pythonmorsels.com › range
Python's range() function - Python Morsels
January 14, 2025 - Let's talk about Python's range function. ... >>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> for n in numbers: ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-range-function
Python range() function - GeeksforGeeks
Example: This example shows the use of range() to generate numbers starting from 0 up to (but not including) a given value. ... Example 1: This example generates numbers starting from a custom value and ending before another value. ... Example 2: This example generates even numbers by skipping values using a custom step size.
Published   March 10, 2026
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - 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(). If you need to count backwards, then you can use a negative step, like range(start, stop, -1), which counts down from start to stop.
🌐
Enki
enki.com › post › how-to-use-python-range-function
Enki | Blog - How to use Python Range Function
With two numbers, range() starts from the first number and stops before the second. This specifies a start and stop, creating a list with numbers 2 through 4.
Find elsewhere
🌐
Tutorial Gateway
tutorialgateway.org › python-range-function
Python range Function
March 31, 2025 - 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) returns 1, 3, 5, 7, and 9.
🌐
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 ...
🌐
w3resource
w3resource.com › python › built-in-function › range.php
Python range() function - w3resource
April 18, 2026 - Example-1: Python range() function · # empty range print(list(range(0))) # using range(stop) print(list(range(12))) # using range(start, stop) print(list(range(1, 15))) Output: [] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] Pictorial Presentation: Pictorial Presentation: Example-2: Python range() function ·
🌐
ThePythonGuru
thepythonguru.com › python-builtin-functions › range
Python range() function - ThePythonGuru.com
The syntax of the range() function is as follows: Syntax: range([start,] stop [, step]) -> range object · Let's now look at a couple of examples to understand how range() works: Example 1: Try it out: print(range(5)) # list() call is not required in Python 2 print(list(range(5))) Output ·
🌐
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 ·
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-range.html
Python range() Function
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. range(5) returns 0, 1, 2, 3, 4. This is perfect for generating the index numbers into, for example, a string.. >>> s = 'Python' >>> len(s) 6 >>> for ...
🌐
Stack Overflow
stackoverflow.com › questions › 70816436 › meaning-of-the-rangerow-1-1
python - meaning of the range(row,-1,-1) - Stack Overflow
The range() is a built-in function that can accept 3 arguments: start, stop and step. In this case, row and col are the starting argument. And the iteration will go until zero (the stop value, -1, is not included) with step -1. And zip groups ...
🌐
Mimo
mimo.org › glossary › python › range-function
Python range() Function [Python Tutorial]
The Python range() function generates a sequence of numbers in a range. By default, range() starts at 0, increments by 1, and stops before a specified number.
🌐
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.
🌐
Snakify
snakify.org › for loop with range
For loop with range - Learn Python 3 - Snakify
To iterate over a decreasing sequence, we can use an extended form of range() with three arguments - range(start_value, end_value, step). When omitted, the step is implicitly equal to 1. However, can be any non-zero value. The loop always includes start_value and excludes end_value during iteration: ... By default, the function print() prints all its arguments separating them by a space and the puts a newline symbol after it. This behavior can be changed using keyword arguments sep (separator) and end. ... print(1, 2, 3) print(4, 5, 6) print(1, 2, 3, sep=', ', end='. ') print(4, 5, 6, sep=', ', end='. ') print() print(1, 2, 3, sep='', end=' -- ') print(4, 5, 6, sep=' * ', end='.')
🌐
Python Central
pythoncentral.io › pythons-range-function-explained
What Is the Range of the Function | Python for Range | Range() Python
January 27, 2022 - So what's the difference? Well, in Python 2.x range() produced a list, and xrange() returned an iterator - a sequence object.
🌐
IONOS
ionos.com › digital guide › websites › web development › python range
How to use Python range - IONOS
July 18, 2023 - The code below will show you how you can do this with the step parameter. ... Beginning with the number 2, which is used as the starting parameter, every third number up until 10 will be output.