The Three Forms

range(stop)              # 0..stop-1
range(start, stop)       # start..stop-1
range(start, stop, step) # with step
list(range(5))        # [0,1,2,3,4]
list(range(2, 8))     # [2,3,4,5,6,7]
list(range(0, 10, 2)) # [0,2,4,6,8]
list(range(10, 0, -1))# [10,9,…,1]

Why stop Is Exclusive

It keeps loop counts predictable: range(n) produces exactly n values. You can use len(lst) directly as the stop.

Iterating in Reverse

for i in range(len(lst) - 1, -1, -1):
    print(lst[i])
# or simpler:
for item in reversed(lst):
    print(item)

Summing 1..100

total = sum(range(1, 101))  # 5050

Practise this on PyForm — free

PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.

Open PyForm →