๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_range.asp
Python range() Function
Python Examples Python Compiler ... ... 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....
๐ŸŒ
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 CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-range.html
Python range() Function
The range() function can be called in a few different ways shown below (the range feature is implemented as a class, but it's simplest to think of it as a 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 exactly the index numbers for a string length 6, like this:
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-range-function
Python Range() Function Tutorial | DataCamp
September 16, 2024 - You would have scratched your head at least once while trying to reverse a linked list of integer values in C language. However, in Python, it can be achieved with the range() function with just interchanging the start and stop along with adding a negative step size.
๐ŸŒ
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().
Find elsewhere
๐ŸŒ
Python Land
python.land โ€บ home โ€บ language deep dives โ€บ python range() function: how-to tutorial with examples
Python range() Function: How-To Tutorial With Examples โ€ข Python Land
June 27, 2023 - There are three ways to call the range function: # With one integer argument it # counts from 0 to stop range(stop) # With two integer arguments it # counts from start to stop range(start, stop) # With three integer arguments it # counts from start to stop, # with a defined step size range(start, stop, step)Code language: Python (python)
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python range() explained with examples
Python range() Function Explained with Examples
March 17, 2022 - The range() is a built-in function that returns a range object that consists series of integer numbers, which we can iterate using a for loop. In Python, Using a for loop with range(), we can repeat an action a specific number of times.
๐ŸŒ
Python Basics
pythonbasics.org โ€บ home โ€บ python basics โ€บ python range() function explained
Python Range() function explained - pythonbasics.org
They can be both positive and negative. By default, it creates a list of numbers starting from zero, as parameter the stop value is defined ... But you can define the starting number of the sequence and then step size. ... Lets say you want to create a list of 100 numbers.
Top answer
1 of 6
54

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)].

๐ŸŒ
docs.python.org
docs.python.org โ€บ 3 โ€บ library โ€บ functions.html
Built-in Functions โ€” Python 3.14.4 documentation
Added in version 3.2: This function was first removed in Python 3.0 and then brought back in Python 3.2. ... Return the string representing a character with the specified Unicode code point. For example, chr(97) returns the string 'a', while chr(8364) returns the string 'โ‚ฌ'. This is the inverse of ord(). The valid range ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-range-function-explained-with-code-examples
Python range() Function โ€“ Explained with Code Examples
October 6, 2021 - In Python, can use use the range() function to get a sequence of indices to loop through an iterable. You'll often use range() in conjunction with a for loop. In this tutorial, you'll learn about the different ways in which you can use the range() ...
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-range
How to Use Range in Python | Coursera
By default, Python follows these rules when defining the sequence: It begins with 0. It advances in increments of 1. It does not include the final number in the specified range. You can change the parameters of your range if the default settings donโ€™t suit your needs. There are three parameter values for the range() function: 1.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-range-function-example
Python range() Function Example
March 17, 2023 - Python's built-in range() function is mainly used when working with for loops โ€“ you can use it to loop through certain blocks of code a specified number of times.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_for_range.asp
Python Looping Through a Range
Python Examples Python Compiler ... ... The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number....
๐ŸŒ
Enki
enki.com โ€บ post โ€บ how-to-use-python-range-function
Enki | Blog - How to use Python Range Function
The Python range() function is an essential tool for generating sequences of numbers. If you're working with loops or iterations, this function is your go-to. It helps you manage the flow of your code and handle repetitive tasks efficiently.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ range
Python's range() function - Python Morsels
January 14, 2025 - You can think of the three arguments given to range as similar to the three values we can use when slicing a sequence in Python: >>> message = "value exalt apprise esteem" >>> message[6:19:2] 'eatapie' These indices that we give are: the start, the stop, and the step indices. Just like with a range function, we have a start value, a stop value, and a step value:
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_range_function.htm
Python range() Function
The Python range() function returns an immutable sequence of numbers within the specified range. This function is used to iterate or loop through a particular code block till a given number of times.