DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Range in Python (1)

Buy Me a Coffee

*Memo:

  • My post explains a range (2).
  • My post explains a range (3).
  • My post explains a range (4).
  • My post explains range functions.
  • My post explains 10 collection types and their related posts.

A range:

  • is the ordered immutable(hashable) collection of zero or more integers whose type is range:
    • Ordered means that the order of the integers in a range is kept so it guarantees that the order is always the same unless someone or something changes it.
    • Immutable(Hashable) means the integers of a range cannot be changed.
  • can be used with len() to get the length.
  • is True if it's non-empty and False if it's empty, checking it with bool().
  • is False if it's non-empty and True if it's empty, inverting the truth value with not.
  • can be checked if a specific number is and isn't in the range with in and not in respectively.
  • can be checked if the range is and isn't referred to by two variables with is and is not respectively.
  • and other range can be checked if all the integers in them are and aren't the same with == and != respectively.
  • and other range cannot be checked if:
    • the range is greater than other range with >.
    • the range is greater than or equal to other range with >=.
    • the range is less than other range with <.
    • the range is less than or equal to other range with <=.
  • and other range cannot be checked if they have and don't have their common integers with bool() and & and with not and & respectively.
  • cannot be enlarged with * and a number.
  • and other ranges cannot be concatenated with +.
  • and other range cannot return:
    • all the integers in them with '|' (Union: A ∪ B).
    • their common integers with '&' (Intersection: A ∩ B).
    • the integers in the range which aren't in other range with '-' (Difference: A - B).
    • the integers in either the range or other range but not both with '^' (Symmetric Difference: A Δ B).
  • can be iterated with a for statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be created by range() in the range [start, stop).
  • can be big because it's the special object which always uses small memory not to get MemoryError and it's not an iterator.
  • can be read by indexing and slicing.
  • cannot be changed by indexing, slicing and a del statement.
  • can be continuously used through multiple variables.
  • can be shallow-copied only by slicing but not by copy.copy().
  • cannot be deep-copied and even shallow-copied by copy.deepcopy().

Even a big range doesn't get MemoryError.


range() can create a range in the range [start, stop) as shown below:

*Memo:

  • The 1st argument is start or stop(Required-Type:int):
    • It's a start index(inclusive) or stop index(exclusive).
    • Don't use start= or stop=.
  • The 2nd argument is stop(Optional-Type:int):
    • It's a stop index(exclusive).
    • Don't use stop=.
  • The 3rd argument is step(Optional-Default:1-Type:int):
    • It's the interval of elements.
    • It cannot be 0.
    • Don't use step=.
  • Only if one argument is set, the 1st argument is stop, creating the range in the range [0, stop).
  • start and stop can be signed indices(zero and positive and negative indices).
# Empty range
v = range(0)
v = range(0, 0)
v = range(0, 0, 1)
v = range(5, 5)
v = range(5, 5, 1)
v = range(-10)
v = range(10, -10)
v = range(-10, 10, -3)
v = range(10, -10, 3)
# No error

v = range(5)
v = range(0, 5)
v = range(0, 5, 1)
v = range(-10, 10, 3)
v = range(10, -10, -3)
# No error

print(len(range(5)))
print(bool(range(1)))
print(bool(range(0)))
print(not range(1))
print(not range(0))
print(0 in range(3))
print(0 not in range(3))
print(range(3) is range(3))
print(range(3) is not range(3))
print(range(3) == range(3))
print(range(3) != range(3))
for x in range(5): print(x)
v1, v2, v3 = range(3); print(v1, v2, v3)
v1, *v2, v3 = range(6); print(v1, v2, v3)
for v1, v2, v3 in [range(3), range(3, 6)]: print(v1, v2, v3)
for v1, *v2, v3 in [range(6), range(6, 12)]: print(v1, v2, v3)
print(*range(4), *range(4, 6))
print([*range(4), *range(4, 6)])
print(range(100000000))
# No error

print(range(0, 5, 0))
print(range(3) > range(3))
print(range(3) >= range(3))
print(range(3) < range(3))
print(range(3) <= range(3))
print(bool(range(3) & range(1, 4, 2)))
print(not (range(3) & range(1, 4, 2)))
print(range(5) * 3)
print(range(3) + range(3, 5) + range(5, 9))
print(range(0, 5, 4) | range(0, 5, 2))
print(range(4) & range(0, 5, 2))
print(range(4) - range(0, 5, 2))
print(range(4) ^ range(0, 5, 2))
# Error
Enter fullscreen mode Exit fullscreen mode

A range is the ordered immutable(hashable) collection of zero or more integers whose type is range as shown below:

*Memo:

  • A range can be unpacked with *.
v = range(5)
v = range(0, 5)
v = range(0, 5, 1)

print(v)
# range(0, 5)

print(type(v))
# <class 'range'>

print(v.start, v.stop, v.step)
# 0 5 1

print(*v)
print(v[0], v[1], v[2], v[3], v[4])
# 0 1 2 3 4

v[1] = 10
v[3] = 30
# TypeError: 'range' object does not support item assignment
Enter fullscreen mode Exit fullscreen mode
v = range(0, 5, 0)
# ValueError: range() arg 3 must not be zero
Enter fullscreen mode Exit fullscreen mode
# Empty range
v1 = range(0)
v2 = range(0, 0)
v3 = range(0, 0, 1)
v4 = range(5, 5)
v5 = range(5, 5, 1)

print(v1, v2, v3, v4, v5)
# range(0, 0) range(0, 0) range(0, 0) range(5, 5) range(5, 5)

print(v[0])
# IndexError: range object index out of range
Enter fullscreen mode Exit fullscreen mode
v = range(-10, 10, 3)

print(v)
# range(-10, 10, 3)

print(*v)
print(v[0], v[1], v[2], v[3], v[4], v[5], v[6])
# -10 -7 -4 -1 2 5 8
Enter fullscreen mode Exit fullscreen mode
v = range(10, -10, -3)

print(v)
# range(10, -10, -3)

print(*v)
print(v[0], v[1], v[2], v[3], v[4], v[5], v[6])
# 10 7 4 1 -2 -5 -8
Enter fullscreen mode Exit fullscreen mode
v1 = range(-10)
v2 = range(10, -10)
v3 = range(-10, 10, -3)
v4 = range(10, -10, 3)

print(v1, v2, v3, v4)
# range(0, -10) range(10, -10) range(-10, 10, -3) range(10, -10, 3)

print(v1[0])
print(v2[0])
print(v3[0])
print(v4[0])
# IndexError: range object index out of range
Enter fullscreen mode Exit fullscreen mode

Top comments (0)