Write a function that generates numbers like Python’s range function, but the last number is included in the interval. This function takes three parameters, and its behavior changes if we pass one, two, or three
parameters. Call it a myrange.
Examples:
list(myrange(1))
[0, 1]
list(myrange(1, 10))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list(myrange(0, 10, 2))
[0, 2, 4, 6, 8, 10]
You may have noticed that, unlike range, the myrange function considers the end of the interval as closed; the last number is part of the range.
Answer:
##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 08/exercise-08-21.py.py
##############################################################################
def myrange(start, end=None, step=1):
if end is None:
start, end = 0, start
current = start
while current <= end: # Note the <= to include the last value
yield current
current += step
# Test cases
print(list(myrange(1))) # [0, 1]
print(list(myrange(1, 10))) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list(myrange(0, 10, 2))) # [0, 2, 4, 6, 8, 10]