For practice I wrote a function to generate a list of numbers from a start to an end by a step. I couldn't make it- like the built-in range
- default start
to 0, because all optional parameters must come after required ones, so def range(start=0, end, step=1)
won't work.
Here's my script:
def Range(start, end, step=1):
"""Returns a list of numbers beginning from start and
incremented by step to end (with end excluded).
start: an integer specifies the start of the range.
end: an integer specifies the end of the range.
step: an integer specifies the number to increment by.
"""
num = start
result = []
while num < end:
result.append(num)
num += step
return result
How can it be refactored and optimized ?
Notes:
- I'm a hobbyist and beginner (in Python in particular, and programming in general).