Range functions in Python

Buy Me a Coffee☕

*Memo:

My post explains a range (1).

index() can get the index of the element matched to value from the range as shown below:

*Memo:

The 1st argument is value(Required-Type:Any).
Error occurs if value doesn’t exist.

v = ra…


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

Buy Me a Coffee

*Memo:

index() can get the index of the element matched to value from the range as shown below:

*Memo:

  • The 1st argument is value(Required-Type:Any).
  • Error occurs if value doesn't exist.
v = range(5, 10)

print(*v)
# 5 6 7 8 9

print(v.index(7))  # 2
print(v.index(10)) # ValueError: 10 is not in range

count() can count the elements matched to value in the range as shown below:

*Memo:

  • The 1st argument is value(Required-Type:Any):
    • Don't use value=.
v = range(5, 10)

print(*v)
# 5 6 7 8 9

print(v.count(7))  # 1
print(v.count(10)) # 0

sorted() can convert a range to a list, then sort the list as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable):
    • Don't use iterable=.
  • The 2nd argument is key(Optional-Default:None-Type:Callable or NoneType).
  • The 3rd argument is reverse(Optional-Default:False-Type:bool) to reverse the list.
  • sorted() creates a copy:
    • Be careful, sorted() does shallow copy instead of deep copy as my issue.
v = range(-3, 3)

print(*v)
# -3 -2 -1 0 1 2

print(sorted(v))
print(sorted(v, key=None, reverse=False))
# [-3, -2, -1, 0, 1, 2]

print(sorted(v, reverse=True))
# [2, 1, 0, -1, -2, -3]

print(sorted(v, key=abs))
# [0, -1, 1, -2, 2, -3]

print(sorted(v, key=abs, reverse=True))
# [-3, -2, 2, -1, 1, 0]

reversed() can return the iterator which has the reversed elements of a range as shown below:

*Memo:

  • The 1st argument is seq(Required-Type:Sequence):
    • Don't use seq=.
v = range(5)

print(*v)
# 0 1 2 3 4

print(reversed(v))
# <range_iterator object at 0x0000023D03BF5030>

print(*reversed(v))
# 4 3 2 1 0


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:09:25+00:00) Range functions in Python. Retrieved from https://www.scien.cx/2025/10/12/range-functions-in-python/

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

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.