Question about range function: advanced-python-code-challenges-lists

Hi guys, iā€™m trying to complete this practice but i have become confused between the use of range() function or myList[START_NUMBER:END_NUMBER], when do you use each one?

thanks for the help :slight_smile:

1 Like


Range is a bread and butter tool in python. One of its common uses is to pair with for loops. You will need them accordingly for different contexts.

Notice the difference in these examples

list = ['pita','bread','sandwich']

for i in list:
# pita
# bread
# sandwhich

for i in range(len(list)):
# 0
# 1
# 2

# bread

# ['bread', 'sandwich']
# extra word of caution, the end number is exclusive! so although we don't have
# list[3], writing the 3 in list[1:3] actually calls for index 2!

# ['bread']
# note the difference with list[1]!!!
1 Like