Demonstration of how a Python range works (Python 3 )

Demonstration of how a Python range works

A range is an iterable object, and this object can return an iterator that keeps track of its current state.

Suppose we create a range with the call range(3). This returns an object of type range with our requested settings. Internally, a Python range will set the following internal variables:

When we start iterating a range object, the object will return an iterator. This iterator uses the values defined above, but has one extra: a counter i, initialized to 0 by default. It increases on each iteration.

On each call to the iterator to get the next number, range looks at its internal state and calculates the next number to return. The next value is calculated like this:

next = start + step*i

Edit and run this Python 3 example code

This code is editable and runnable. You can run "Demonstration of how a Python range works" by pressing the run button. It will be executed in our backend and the result (stdout and stderr) is displayed in addition tabs.

Share "Demonstration of how a Python range works"

Share this crumb (Demonstration of how a Python range works) with your friends and colleagues to help them learn more about Python 3 programming.

Twitter, Facebook, LinkedIn

On Twitter, Facebook, and LinkedIn, simply paste the URL in your post and watch the magic unfold.


If you want to embed the crumb in an iframe, use the following code:

oEmbed (WordPress)

We support oEmbed, but most sites use allowlisting. On WordPress, oEmbed is supported out of the box, but see the note below.

If you plan to use heavily on Wordpress, you can use the following PHP code to allowlist our site and unlock all the functionalities of our crumbs:
wp_oembed_add_provider( '*', '' ); is a service that allows you to embed content from other sites. We will request to be added to the list of trusted sites once we're out of beta.