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

Leetcode — LRU cache

15 jokes only programmers will get

WEEKLY SUMMARY 5

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

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