range in Python (2)

Buy Me a Coffee☕

*Memo:

My post explains a range (1).

A range can be read by slicing as shown below:

*Memo:

Slicing can be done with one or more [start:end:step]:

start(Optional-Default:The index of the 1st element):

It’s a start index(in…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*Memo:

A range can be read by slicing as shown below:

*Memo:

  • Slicing can be done with one or more [start:end:step]:
    • start(Optional-Default:The index of the 1st element):
      • It's a start index(inclusive).
    • end(Optional-Default:The index of the last element + 1):
      • It's an end index(exclusive).
    • step(Optional-Default:1):
      • It's the interval of indices.
      • It cannot be zero.
    • The [] with at least one : is slicing.
v1 = range(10)

print(v1)
# range(0, 10)

print(*v1)
# 0 1 2 3 4 5 6 7 8 9

v2 = v1[2:8]
v2 = v1[-8:-2]

print(v2)
# range(2, 8)

print(*v2)
# 2 3 4 5 6 7

v2 = v1[2:8:2]
v2 = v1[-8:-2:2]

print(v2)
# range(2, 8)

print(*v2)
# 2 4 6

A range cannot be changed by indexing or slicing as shown below:

*Memo:

  • A del statement can still be used to remove one or more variables themselves.
v = range(5)

v[0] = 10
v[2:5] = [20, 30]
# TypeError: 'range' object does not support item assignment
v = range(5)

del v[0], v[2:5]
# TypeError: 'range' object does not support item deletion
v = range(5)

del v

print(v)
# NameError: name 'v' is not defined

A range can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = range(3)

print(v1, v2, v3)
# 0 1 2
v1, *v2, v3 = range(6)

print(v1, v2, v3)  # 0 [1, 2, 3, 4] 5
print(v1, *v2, v3) # 0 1 2 3 4 5
for v1, v2, v3 in [range(3), range(3, 6)]:
    print(v1, v2, v3)
# 0 1 2
# 3 4 5
for v1, *v2, v3 in [range(6), range(6, 12)]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11
print(*range(4), *range(4, 6))
# 0 1 2 3 4 5
print([*range(4), *range(4, 6)])
# [0, 1, 2, 3, 4, 5]
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*range(4), *range(4, 6))
# 0 1 2 3 4 5
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, ['A', 'B', *args, 'C', 'D'])

func()
# a b ()
# a b Nothing
# a b ['A', 'B', 'C', 'D']

func(*range(4), *range(4, 6))
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']

A range can be continuously used through multiple variables as shown below:

v1 = v2 = v3 = range(5)                # Equivalent
                                       # v1 = range(5)
print(v1, *v1) # range(0, 5) 0 1 2 3 4 # v2 = v1
print(v2, *v2) # range(0, 5) 0 1 2 3 4 # v3 = v2
print(v3, *v3) # range(0, 5) 0 1 2 3 4

A range can be shallow-copied (only by slicing) but cannot deep-copied as shown below:

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different ranges (only by slicing) and each same element.
  • is keyword can check if v1 and v2 refer to the same range and/or each same element.
  • Slicing can shallow-copy the range.
  • copy.copy() cannot shallow-copy a range.
v1 = range(5)
v2 = v1[:]

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# False True
import copy

v1 = range(5)
v2 = copy.copy(v1)

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# True True

<Deep copy>:

*Memo:

  • v1 and v2 refer to the same range and each same element.
  • copy.deepcopy() cannot deep-copy and even shallow-copy a range.
import copy

v1 = range(5)
v2 = copy.deepcopy(v1)

print(v1, *v1) # range(0, 5) 0 1 2 3 4
print(v2, *v2) # range(0, 5) 0 1 2 3 4

print(v1 is v2, v1[2] is v2[2])
# True True


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-10-12T05:00:47+00:00) range in Python (2). Retrieved from https://www.scien.cx/2025/10/12/range-in-python-2/

MLA
" » range in Python (2)." Super Kai (Kazuya Ito) | Sciencx - Sunday October 12, 2025, https://www.scien.cx/2025/10/12/range-in-python-2/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday October 12, 2025 » range in Python (2)., viewed ,<https://www.scien.cx/2025/10/12/range-in-python-2/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » range in Python (2). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/12/range-in-python-2/
CHICAGO
" » range in Python (2)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/10/12/range-in-python-2/
IEEE
" » range in Python (2)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/10/12/range-in-python-2/. [Accessed: ]
rf:citation
» range in Python (2) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/10/12/range-in-python-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.