range
range()
constructor
range(5)
# => range(0, 5)
for i in range(5):
print(i)
# => 0
# => 1
# => 2
# => 3
# => 4
### using starting value
range(5, 10)
# => range(5, 10)
list(range(5, 10))
# => [5, 6, 7, 8, 9]
### using step argument
list(range(0, 10, 2))
# => [0, 2, 4, 6, 8]
range()
Signaturerange(stop)
range(start, stop)
range(start, stop, step)
range
does not support keyword argumentspoorly-styled example:
s = [0, 1, 4, 6, 13]
for i in range(len(s)):
print(s[i])
# => 0
# => 1
# => 4
# => 6
# => 13
preferable example:
s = [0, 1, 4, 6, 13]
for v in s:
print(v)
# => 0
# => 1
# => 4
# => 6
# => 13
enumerate
(index, value)
tuples around another iterable objectt = [6, 372, 8862, 148800, 2096886]
for p in enumerate(t):
print(p)
# => (0, 6)
# => (1, 372)
# => (2, 8862)
# => (3, 148800)
# => (4, 2096886)
for i, v in enumerate(t):
print(f"i = {i}, v = {v}")
# => i = 0, v = 6
# => i = 1, v = 372
# => i = 2, v = 8862
# => i = 3, v = 148800
# => i = 4, v = 2096886