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
ARG COUNT | ARG MEANING       | COMMENT
----------|-------------------|-------------------------------------
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]

# WRONG
>>> for i in range(len(l)-1,0,-1):print(i)
5
4
3
2
1

# CORRECT
>>> for i in range(len(l)-1,-1,-1):print(i)
5
4
3
2
1
0

3. typical usage: loop counters

  • use range object to count loops
>>> for i in range(7): print(i)
...
0
1
2
3
4
5
6
  • 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: https://pavol.kutaj.com

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

Recommended from Medium

Measuring DevOps Success

Ease of building UI elements in Unity

How to use switch statements

Anarchists, Mercenaries, Farmers, and Ranchers: Engineering Teams at Startups

There are many platforms possible for web application and website development in the application…

What programing languages YOU should learn in 2022

Week of 25 Oct — 31 Oct: John Jin

A Metaverse Manifesto, Part 7: One Family Tree. One Metaverse.

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: https://pavol.kutaj.com

More from Medium

How __Main__.Py Can Be Used To Execute Directory With Python

6 Ways You Can Get Keys From a Python Dictionary

10 Useful String Operations in Python

Reverse-Engineered “Wordle” with 10 lines of Python