for passnum in range(len(alist)-1,0,-1):
Per the range function documentation:
len(alist) - 1
is the length of the list (8) minus one (7). It represents some “starting” number.0
represents the “stopping” number.-1
represents the “step” number.
These come together in range(len(alist) - 1, 0, -1)
to say “count from 7 to 0, backwards – one negative step at a time”.
for i in range(passnum):
When range()
is only passed one argument (a number), it counts from 0 to that number. The documentation gives this example:
list(range(10))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
So the code counts from 7 down to 0 – and every time it performs a single count, it immediately counts from 0 to that number.
solved Bubble sort function in Python