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:

https://www.codecademy.com/paths/data-science/tracks/dspath-python-lists-and-loops/modules/dspath-lists/articles/advanced-python-code-challenges-lists

1 Like

Hi!

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:
    print(i)
# pita
# bread
# sandwhich

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

print(list[1])
# bread

print(list[1:3]) 
# ['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!

print(list[1:2]) 
# ['bread']
# note the difference with list[1]!!!
1 Like