The Parameters And Usage Of Ranges In Python

1. what is a range

  • range is a sequence that represents an arithmetic progression of integers
  • in Python it is created by range() built-in function — there is no literal form
  • NOTE: the end value is one past the stop value of a sequence — same as in slicing, it is utilizing half open interval convention
  • the start value is usually 0 — but can be optionally passed into the range constructor
  • range is strange because it determines what its argument means by counting them
  • i.e., arguments are not it is not positional
  • and it also does not support keyword args (a bit un-pythonic as this is a special case breaking the rule)
  • it counts the arguments and concludes the meaning as follows
1 | stop | counting from zero
2 | start, stop | using the half-open interval (!)
3 | start, stop, step | an increment
  • as hinted above, to use step argument, you have to supply all 3 arguments
>>> list(range(0,18,3))
[0, 3, 6, 9, 12, 15]

2. why half-open range convention and the three times -1 range arguments to parse a list backward

  • for working with consecutive ranges
  • the end specified in one range is the beginning of the next range
  • there is no overlap
>>> list(range(1,4))
[1, 2, 3]
>>> list(range(4,8))
[4, 5, 6, 7]
  • but it can get a little weird
  • e.g. in the linked leet code example you want simply to loop through indexes of a list in a decreasing order
  • you have to have -1 as a stop parameter if you want to stop at index 0 (which is what you want with standard 0-based indexing), something like this:
>>> l = list(range(6))
>>> l
[0, 1, 2, 3, 4, 5]

>>> for i in range(len(l)-1,0,-1):print(i)

>>> for i in range(len(l)-1,-1,-1):print(i)

3. typical usage: loop counters

  • use range object to count loops
>>> for i in range(7): print(i)
  • specifies the number of times that the suite should be executed (3 times in the loop below)
  • if an item will not appear in any expression, it is conventional to use underscore (_) in for loop header
for _ in range(3):
print("Go Nuts")

>>> Go Nuts!
>>> Go Nuts!
>>> Go Nuts!

4. usage: list/tuple constructor

  • pass a range constructor into a list constructor to get the half-open range
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> tuple(range(5))
(0, 1, 2, 3, 4)

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me:

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Leetcode — LRU cache

15 jokes only programmers will get


docker MongoDB Multiple Databases

When OOP Works and When It Doesn’t

Running your WordPress site in containers — 2018 version

Provisioning Infrastructure for Stateful Services in Public Cloud: An HBase Use Case (Part II)

Pair Programming

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me:

More from Medium

Byte Strings Are Decoded To ASCII In IO By Default In Python

Get Started with Python in VSCode on Debian and Ubuntu-based distributions

Drawing Heart using Python

Python Conditional Statment : If, If Else, Elif