In: Computer Science
r = range(10, 40, 3)
def print_lv(strings):
strings = strings if isinstance(strings, list) else [strings]
for string in strings:
st = "f'" + string + ": {" + string + "}'"
print_stmt = 'print(' + st + ', end="; ")'
# Uncomment the following print statement to see the statement to be executed.
# Each will appear on a separate line.
# print(f'\n{print_stmt}')
exec(print_stmt)
print()
print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])'])
OUTPUT:
list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37]; r[-2:3:-1]: range(34, 19, -3); list(r[-2:3:-1]): [34, 31, 28, 25, 22];
I need help explaining this bit of code. Particularly why r[-2:3:-1] is range(34, 19, -3).
The slice starts at position -2 and stops at position 3 in increments of step -1.
YET the range that results from taking that slice starts at value 34 and stops at value 19 in increments of step -3