-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_range.py
48 lines (40 loc) · 972 Bytes
/
my_range.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Learn about range() Collection
"""
def main():
"""
Test function
:return:
"""
# Default
for i in range(5):
print(i)
# Set the initial value: =5 to <10
print("Now setting initial value")
for i in range(5, 10):
print(i)
# Create a list from range
l = list(range(5, 10))
print(l, type(l))
l2 = list(range(5, 10)) + list(range(30,40))
print(l2, type(l2))
# Step argument: (begin, stop, step)
l3 = list(range(0, 20, 2))
print(l3, type(l3))
# Iteration over list using index notation
s = [0, 2, 3, 4, 6]
for i in range(len(s)):
print(s[i])
# Better way
for v in s:
print(v)
# enumerate(): returns an iterable series
t = [6, 789, 123, 98, 3, 22]
for p in enumerate(t):
print(p, p[0], p[1])
# A better
for i, v in enumerate(t):
print("i = {}, v={}".format(i, v))
if __name__ == '__main__':
main()
exit(0)